Try
[알고리즘/자료구조] 그래프 (Graph) -1- 인접리스트 구현 본문
그래프
정점의 모음과 이 정점을 잇는 간선의 모음의 결합
정점의 집합을 V, 간선의 집합을 E, 그래프를 G라 했을때 G = (V, E)
인접리스트와 인접 행렬로 구현할 수 있다.
예시
행렬로 구현한 예
리스트로 구현한 예
소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /* /* khsh5592@naver.com /* has3ong.tistory.com /* /* 2018 - 12 - 09 /* */ #include <stdio.h> #include <iostream> #include <conio.h> using namespace std; class Vertex { public: int Data; int index; class Edge* edge_list; Vertex* next; Vertex* vertex_Create(int newData, int index); }; class Edge { public: int weight; Edge* next; Vertex* from; Vertex* to; Edge* edge_Create(Vertex* from, Vertex* to, int weight); }; class Graph { public: Vertex* Vertex_list; int cnt; Graph* graph_Create(); void add_Vertex(Graph* G, Vertex* V); void add_Edge(Vertex* V, Edge* E); }; Graph* graph_Create() { Graph* G = new Graph; G -> Vertex_list = NULL; G -> cnt = 0; return G; } Vertex* vertex_Create(int newData, int index) { Vertex* V = new Vertex; V -> Data = newData; V -> index = index; V -> next = NULL; V -> edge_list = NULL; return V; } Edge* edge_Create(Vertex* from, Vertex* to, int weight) { Edge* E = new Edge; E -> weight = weight; E -> from = from; E -> to = to; E -> next = NULL; return E; } void add_Vertex(Graph* G, Vertex* V) { Vertex* list = G -> Vertex_list; if (list == NULL) { G -> Vertex_list = V; } else { while (list -> next != NULL) { list = list -> next; } list -> next = V; } G -> cnt++; } void add_Edge(Vertex* V, Edge* E) { if (V -> edge_list == NULL) { V -> edge_list = E; } else { Edge* list = V -> edge_list; while (list -> next != NULL) { list = list -> next; } list -> next = E; } } void print(Graph* G) { Vertex* V = NULL; Edge* E = NULL; if(G -> Vertex_list == NULL) { return; } else { V = G -> Vertex_list; while( V != NULL) { cout << "Index : " << V -> index << " Data : " << V-> Data; if (V -> edge_list == NULL) { } else { E = V -> edge_list; while(E != NULL) { cout << " [ " << E->from << ", " << E -> to << " ] = " << E->weight; E = E -> next; } } cout << endl; V = V -> next; } } } void main() { Graph* G = graph_Create(); Vertex* vertex0 = vertex_Create(0, 0); Vertex* vertex1 = vertex_Create(1, 1); Vertex* vertex2 = vertex_Create(2, 2); Vertex* vertex3 = vertex_Create(3, 3); add_Vertex(G, vertex0); add_Vertex(G, vertex1); add_Vertex(G, vertex2); add_Vertex(G, vertex3); Edge* edge0 = edge_Create(vertex0, vertex1, 1); Edge* edge1 = edge_Create(vertex0, vertex2, 2); Edge* edge2 = edge_Create(vertex1, vertex0, 3); Edge* edge3 = edge_Create(vertex1, vertex2, 4); Edge* edge4 = edge_Create(vertex2, vertex0, 5); Edge* edge5 = edge_Create(vertex2, vertex1, 6); Edge* edge6 = edge_Create(vertex2, vertex3, 7); Edge* edge7 = edge_Create(vertex3, vertex2, 8); add_Edge(vertex0, edge0); add_Edge(vertex0, edge1); add_Edge(vertex1, edge2); add_Edge(vertex1, edge3); add_Edge(vertex2, edge4); add_Edge(vertex2, edge5); add_Edge(vertex2, edge6); add_Edge(vertex3, edge7); print(G); getch(); } | cs |
결과화면
'Algorithm > Algorithm 기초' 카테고리의 다른 글
[알고리즘/자료구조] 그래프 (Graph) -2- 인접행렬 구현 (0) | 2018.12.09 |
---|---|
[알고리즘/자료구조] 해시 테이블(Hash Table) (0) | 2018.11.29 |
[알고리즘/자료구조] 우선순위 큐 (PriorityQueue) (0) | 2018.11.29 |
[알고리즘/자료구조] 힙 (Heap) (0) | 2018.11.29 |
[알고리즘/자료구조] 이진 탐색 트리 (Binary Search Tree) (0) | 2018.11.29 |
Comments