티스토리 뷰
반응형
알고리즘의 꽃, 그래프.
G=(V,E) 로 표현한다.
V는 정점을, E는 간선을 의미한다.
크게 방향성 유무(Directed,Undirected)와 사이클 존재 유무(Cyclic,Acyclic)로 구분하는 듯 하다.
최소신장트리 알고리즘, 순회 알고리즘 등 많은 알고리즘이 그래프에서 파생되어 나온다.
1. Graph.h
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 | #ifndef GRAPH_H #define GRAPH_H #include <stdio.h> #include <stdlib.h> enum VisitMode {Visited,NotVisited}; typedef int ElementType; typedef struct tagVertex{ ElementType Data; int VIsited; int Index; struct tagVertex* Next; struct tagEdge* AdjacencyList; }Vertex; typedef struct tagEdge{ Vertex* From; Vertex* Target; int Weight; struct tagEdge* Next; }Edge; typedef struct tagGraph{ Vertex* Vertices; int VertexCount; }Graph; Graph* CreateGraph(); void DestroyGraph(Graph* G); Vertex* CreateVertex(ElementType Data); void DestroyVertex(Vertex* V); Edge* CreateEdge(Vertex* From,Vertex* Target,int Weight); void DestroyEdge(Edge* E); void AddVertex(Graph* G,Vertex* V); void AddEdge(Vertex* V,Edge* E); void PrintGraph(Graph* G); #endif | cs |
2. Graph.c
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 | #include "Graph.h" Graph* CreateGraph(){ Graph* graph=(Graph*)malloc(sizeof(Graph)); graph->Vertices=NULL; graph->VertexCount=0; return graph; } void DestroyGraph(Graph* G){ while(G->Vertices!=NULL){ Vertex* Vertices=G->Vertices->Next; DestroyVertex(G->Vertices); G->Vertices=Vertices; } free(G); } Vertex* CreateVertex(ElementType Data){ Vertex* V=(Vertex*)malloc(sizeof(Vertex)); V->Data=Data; V->AdjacencyList=NULL; V->Index=-1; V->Next=NULL; V->VIsited=NotVisited; return V; } void DestroyVertex(Vertex* V){ while(V->AdjacencyList!=NULL){ Edge* Edge=V->AdjacencyList->Next; DestroyEdge(V->AdjacencyList); V->AdjacencyList=Edge; } free(V); } Edge* CreateEdge(Vertex* From,Vertex* Target,int Weight){ Edge* E=(Edge*)malloc(sizeof(Edge)); E->From=From; E->Target=Target; E->Weight=Weight; E->Next=NULL; return E; } void DestroyEdge(Edge* E){ free(E); } void AddVertex(Graph* G,Vertex* V){ Vertex* VertexList=G->Vertices; if(VertexList==NULL){ G->Vertices=V; } else{ while(VertexList->Next!=NULL){ VertexList=VertexList->Next; } VertexList->Next=V; } V->Index=G->VertexCount++; } void AddEdge(Vertex* V,Edge* E){ if(V->AdjacencyList==NULL){ V->AdjacencyList=E; } else{ Edge* AdjacencyList=V->AdjacencyList; while(AdjacencyList->Next!=NULL){ AdjacencyList=AdjacencyList->Next; } AdjacencyList->Next=E; } } void PrintGraph(Graph* G){ Vertex* V=NULL; Edge* E=NULL; if((V=G->Vertices)==NULL){ return; } while(V!=NULL){ printf("%c : ",V->Data); if((E=V->AdjacencyList)==NULL){ V=V->Next; printf("\n"); continue; } while(E!=NULL){ printf("%c[%d] ",E->Target->Data,E->Weight); E=E->Next; } printf("\n"); V=V->Next; } printf("\n"); } | cs |
3. Test_Graph.c
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 | #include "Graph.h" int main(){ Graph* G=CreateGraph(); /*정점 추가*/ Vertex* V1=CreateVertex('1'); Vertex* V2=CreateVertex('2'); Vertex* V3=CreateVertex('3'); Vertex* V4=CreateVertex('4'); Vertex* V5=CreateVertex('5'); /*그래프에 정점 추가*/ AddVertex(G,V1); AddVertex(G,V2); AddVertex(G,V3); AddVertex(G,V4); AddVertex(G,V5); /* 정점과 정점을 간선으로 잇기 */ AddEdge(V1,CreateEdge(V1,V2,0)); AddEdge(V1,CreateEdge(V1,V3,0)); AddEdge(V1,CreateEdge(V1,V4,0)); AddEdge(V1,CreateEdge(V1,V5,0)); AddEdge(V2,CreateEdge(V2,V1,0)); AddEdge(V2,CreateEdge(V2,V3,0)); AddEdge(V2,CreateEdge(V2,V5,0)); AddEdge(V3,CreateEdge(V3,V1,0)); AddEdge(V3,CreateEdge(V3,V2,0)); AddEdge(V4,CreateEdge(V4,V1,0)); AddEdge(V4,CreateEdge(V4,V5,0)); AddEdge(V5,CreateEdge(V5,V1,0)); AddEdge(V5,CreateEdge(V5,V2,0)); AddEdge(V5,CreateEdge(V5,V4,0)); PrintGraph(G); DestroyGraph(G); return 0; } | cs |
실행 결과
반응형
'개인 서재..* > 알고리즘' 카테고리의 다른 글
Parametric Search(파라메트릭 서치) (0) | 2017.06.01 |
---|---|
[C언어] BFS와 DFS (0) | 2017.05.10 |
[C언어] qsort()와 bsearch() 사용하기 (0) | 2017.04.25 |
[C언어] 이진 탐색 (Binary Search) (0) | 2017.04.25 |
[C언어] 자기 구성 순차 탐색법 (0) | 2017.04.25 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 스파투어 파트4
- 구글애드센스 광고차단
- 스텐팬 갈색
- 싱크대 단수
- 피지낭종 제거수술
- 휘센 제습기
- 아난티앳강남
- 물이 안나와요
- 겨드랑이 초음파
- 겨드랑이 멍울
- 혐오광고 제거
- 타르코프 스파 관광
- 스텐팬 세척
- 사마귀광고 제거
- 피지낭종 수술후기
- 타르코프 스파 투어
- 제습기 후기
- 스파 파트 4
- 스텐팬 갈변
- 자취요리
- 급수에러
- 스텐팬 탄 자국
- 친구들호캉스
- 스파관광 파트4
- 구글애드센스 혐오광고
- 겨드랑이 혹
- 건강검진 겨드랑이
- 피지낭종 후기
- 제습기 내돈내산
- 겨드랑이 지방종
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함