拓扑排序(DFS实现)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdbool.h>
  5 
  6 #define MaxVertexNodeNumSize 1000
  7 #define MaxVertexNodeNameSize 100
  8 
  9 struct VertexBodyNode
 10 {
 11     char VertexName[MaxVertexNodeNameSize];
 12     int ArcWeight;
 13     int VertexIndex;
 14     struct VertexBodyNode *Next;
 15 };
 16 
 17 struct VertexHeadNode
 18 {
 19     char VertexName[MaxVertexNodeNameSize];
 20     int VertexWeight;
 21     struct VertexBodyNode *Next;
 22 };
 23 
 24 struct _Graph
 25 {
 26     struct VertexHeadNode VertexHeadNodeList[MaxVertexNodeNumSize];
 27     int ArcNum,VertexNum;
 28 };
 29 
 30 int VertexName2Index(struct _Graph *DirectedGraph,char *VName)
 31 {
 32     int i;
 33     for(i = 0; i < DirectedGraph -> VertexNum; i ++)
 34     {
 35         if(strcmp(DirectedGraph -> VertexHeadNodeList[i].VertexName,VName)==0)
 36         {
 37             return i;
 38         }
 39     }
 40     return -1;
 41 }
 42 
 43 void AddOneArc(struct _Graph *DirectedGraph,int ArcIndex_1,int ArcIndex_2,int AWeight)
 44 {
 45     struct VertexBodyNode *BNode = malloc(sizeof(struct VertexBodyNode));
 46 
 47     strcpy(BNode -> VertexName,DirectedGraph -> VertexHeadNodeList[ArcIndex_2].VertexName);
 48 
 49     BNode -> ArcWeight = AWeight;
 50     BNode -> VertexIndex = ArcIndex_2;
 51     BNode -> Next = NULL;
 52 
 53     struct VertexBodyNode *TmpPointer;
 54     TmpPointer = DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next;
 55     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
 56     {
 57         TmpPointer = TmpPointer -> Next;
 58     }
 59     if(TmpPointer==NULL)
 60     {
 61         DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next = BNode;
 62     }
 63     else
 64     {
 65         TmpPointer -> Next = BNode;
 66     }
 67 }
 68 
 69 struct _Graph *UGCreat(int ArcSum,int VertexSum)
 70 {
 71     int i,j;
 72     struct _Graph *DirectedGraph = malloc(sizeof(struct _Graph));
 73     DirectedGraph -> ArcNum = ArcSum;
 74     DirectedGraph -> VertexNum = VertexSum;
 75 
 76     for(i = 0; i < VertexSum; i ++)
 77     {
 78         scanf("%s %d",DirectedGraph -> VertexHeadNodeList[i].VertexName,&DirectedGraph -> VertexHeadNodeList[i].VertexWeight);
 79     }
 80 
 81     for(i = 0; i < VertexSum; i ++)
 82     {
 83         DirectedGraph -> VertexHeadNodeList[i].Next = NULL;
 84     }
 85 
 86     for(i = 0; i < ArcSum; i ++)
 87     {
 88         char Arc_1[MaxVertexNodeNameSize];
 89         char Arc_2[MaxVertexNodeNameSize];
 90         int ArcIndex_1;
 91         int ArcIndex_2;
 92         int ArcWeight;
 93 
 94         scanf("%s %s %d",Arc_1,Arc_2,&ArcWeight);
 95 
 96         ArcIndex_1 = VertexName2Index(DirectedGraph,Arc_1);
 97         ArcIndex_2 = VertexName2Index(DirectedGraph,Arc_2);
 98 
 99         AddOneArc(DirectedGraph,ArcIndex_1,ArcIndex_2,ArcWeight);
100     }
101     return DirectedGraph;
102 }
103 
104 void Travel(struct _Graph *DirectedGraph)
105 {
106     char StartingPoint[MaxVertexNodeNameSize];
107     char OverPoint[MaxVertexNodeNameSize];
108 
109     printf("Input start and over\n");
110     scanf("%s %s",StartingPoint,OverPoint);
111 
112     int StartIndex = VertexName2Index(DirectedGraph,StartingPoint);
113     int OverIndex = VertexName2Index(DirectedGraph,OverPoint);
114 
115     struct VertexBodyNode *TmpPointer;
116     TmpPointer = DirectedGraph -> VertexHeadNodeList[StartIndex].Next;
117     while(TmpPointer != NULL)
118     {
119         if(OverIndex==TmpPointer -> VertexIndex)
120         {
121             printf("Distance:%d GetVertexPointSum:%d",TmpPointer->ArcWeight
122                    ,DirectedGraph -> VertexHeadNodeList[StartIndex].VertexWeight+DirectedGraph -> VertexHeadNodeList[OverIndex].VertexWeight);
123             break;
124         }
125         else
126         {
127             TmpPointer = TmpPointer -> Next;
128         }
129     }
130 }
131 
132 void _DFSTSort(struct _Graph *DirectedGraph,int i,bool Visit[],int Output[],int *OutputEnd)
133 {
134     Visit[i] = true;
135 
136     struct VertexBodyNode *TmpPointer = DirectedGraph->VertexHeadNodeList[i].Next;
137     while(TmpPointer != NULL)
138     {
139         if(! Visit[TmpPointer->VertexIndex])
140         {
141             _DFSTSort(DirectedGraph,TmpPointer->VertexIndex,Visit,Output,OutputEnd);
142         }
143         TmpPointer = TmpPointer -> Next;
144     }
145     Output[*OutputEnd] = i;
146     *OutputEnd += 1;
147 }
148 
149 bool DFSTopologicalSort(struct _Graph *DirectedGraph)
150 {
151     bool Visit[DirectedGraph->VertexNum];
152     int Output[DirectedGraph->VertexNum];
153     int OutputEnd = 0;
154     memset(Visit,0,sizeof(Visit));
155 
156     int i;
157     for(i = 0; i < DirectedGraph->VertexNum; i ++)
158     {
159         if(! Visit[i])
160         {
161             _DFSTSort(DirectedGraph,i,Visit,Output,&OutputEnd);
162         }
163     }
164 
165     for(i = OutputEnd-1; i >= 0; i --)
166     {
167         printf("%s ",DirectedGraph -> VertexHeadNodeList[Output[i]].VertexName);
168     }
169 }
170 
171 int main()
172 {
173     struct _Graph *G = UGCreat(8,5);
174 
175 //    Travel(G);
176     DFSTopologicalSort(G);
177     return 0;
178 }
179 
180 /*
181         beijing 18
182         zhengzhou 10
183         hefei 9
184         nanjing 12
185         guangzhou 14
186         beijing zhengzhou 7
187         beijing hefei 9
188         beijing nanjing 8
189         zhengzhou hefei 5
190         hefei nanjing 3
191         zhengzhou guangzhou 7
192         hefei guangzhou 8
193         nanjing guangzhou 6
194 */

 

posted @ 2018-08-05 21:55  Asurudo  阅读(558)  评论(0编辑  收藏  举报