자료구조 & 알고리즘/그래프

// 인접 리스트 그래프의 DFS, BFS class GraphList2 extends GraphList { public GraphList2(int size) { super(size); } public void dfs(int idx) { // && DFS는 배열&스택(인접 정점들을 스택에 넣는다.) boolean[] visited = new boolean[this.vertices.length]; Stack stk = new Stack(); stk.push(idx); visited[idx] = true; while (!stk.isEmpty()) { // 노드 정보 & 간선 정보 분리되어있다는 것 주의 int i = stk.pop(); System.out.print(vertices[i] + " "); //n..
class GraphMatrix2 extends GraphMatrix { public GraphMatrix2(int size) { super(size); } public void dfs(int idx) { // DFS는 배열&스택(인접 정점들을 스택에 넣는다.) boolean[] visited = new boolean[this.vertices.length]; Stack stk = new Stack(); stk.push(idx); visited[idx] = true; while (!stk.isEmpty()) { int i = stk.pop(); System.out.print(vertices[i] + " -> "); for (int j = matrix[i].length - 1; j >= 0; j--) { /..
// 인접 리스트를 이용한 그래프 구현 class Node { int id; // 노드가 id 들고 있음 주의 -> 추후 dfs,bfs 시 방문배열체크용 ! Node next; public Node(int id, Node next) { this.id = id; this.next = next; } } class MyGraphList { char[] vertices; Node[] nodeList; int idx; public MyGraphList(int size) { this.vertices = new char[size]; nodeList = new Node[size]; idx = 0; } public void addVertex(char c) { if (idx == this.vertices.length) {..
class GraphMatrix { char[] vertices; // 노드 int[][] matrix; // 인접 행렬 int idx; public GraphMatrix(int size) { this.vertices = new char[size]; this.matrix = new int[size][size]; this.idx = 0; } public boolean isFull(){ return this.vertices.length == idx; } public void addVertex(char c) { if(isFull()){ System.out.println("Graph is full"); return; } this.vertices[this.idx++] = c; } public void addEdg..
꾸준함의 미더덕
'자료구조 & 알고리즘/그래프' 카테고리의 글 목록