Dijkstra算法實現
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define INFINITY 65535
typedef int DistType;
typedef int Vertex;
typedef int WeightType;
typedef char DataType;
typedef int ElementType;
typedef struct QueueRecord
{
int Front;
int Rear;
int Capacity;
ElementType *Array;
}*Queue;
void InitQueue(Queue Q)
{
Q->Front = 0;
Q->Rear = 0;
}
Queue CreatQueue(int MaxSize)
{
Queue Q = (QueueRecord*)malloc(sizeof(QueueRecord));
Q->Capacity = MaxSize;
Q->Array = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
InitQueue(Q);
return Q;
}
int Enqueue(Queue Q, ElementType X)
{
if ((Q->Rear + 1) % Q->Capacity == Q->Front)
return -1;
Q->Rear = (Q->Rear + 1) % Q->Capacity;
Q->Array[Q->Rear] = X;
return 0;
}
ElementType Dequeue(Queue Q)
{
if (Q->Front == Q->Rear)
return -1;
Q->Front = (Q->Front + 1) % Q->Capacity;
return Q->Array[Q->Front];
}
typedef struct ENode
{
Vertex V1, V2;
WeightType Weight;
}*Edge;
typedef struct GNode
{
int Nv;
int Ne;
WeightType Weight[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];
}*MGraph;
MGraph CreateGraph(int VertexNum)
{
Vertex V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V <= Graph->Nv; V++)
for (W = 0; W <= Graph->Nv; W++)
Graph->Weight[V][W] = INFINITY;
return Graph;
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->Weight[E->V1][E->V2] = E->Weight;
}
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
Vertex V;
int Nv, i;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &(Graph->Ne));
if (Graph->Ne != 0)
{
E = (Edge)malloc(sizeof(ENode));
for (i = 0; i < Graph->Ne; i++)
{
scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
InsertEdge(Graph, E);
}
}
return Graph;
}
typedef struct TableNode
{
int Known;
DistType Distence;
Vertex Path;
}Table;
void InitTable(Vertex Start, MGraph Graph, Table *T)
{
int i;
for (i = 0; i <= Graph->Nv; i++)
{
T[i].Distence = INFINITY;
T[i].Known = 0;
T[i].Path = 0;
}
T[Start].Distence = 0;
}
Vertex FindMinDist(MGraph Graph, Table *T)
{
Vertex MinV, V;
int MinDist = INFINITY;
for (V = 0; V <= Graph->Nv; V++)
{
if (T[V].Known == 0 && T[V].Distence < MinDist)
{
MinDist = T[V].Distence;
MinV = V;
}
}
if (MinDist < INFINITY)
return MinV;
else
return 0;
}
void PrintPath(Vertex V, Table *T)
{
if (T[V].Path != 0)
{
PrintPath(T[V].Path, T);
printf("to ");
}
printf("V%d ", V);
}
void Dijkstra(MGraph Graph, Table *T)
{
Vertex V, W;
for (;;)
{
V = FindMinDist(Graph, T);
if (V == 0)
break;
T[V].Known = 1;
for (W = 0; W <= Graph->Nv; W++)
{
if(Graph->Weight[V][W] != INFINITY && T[W].Known == 0)
if (T[V].Distence + Graph->Weight[V][W] < T[W].Distence)
{
T[W].Distence = T[V].Distence + Graph->Weight[V][W];
T[W].Path = V;
}
}
}
}
int main()
{
MGraph Graph = BuildGraph();
Table T[MaxVertexNum];
InitTable(1,Graph,T);
Dijkstra(Graph, T);
Vertex V;
for (V = 1; V <= Graph->Nv; V++)
{
printf("The distence of V%d is %d,and the path is ", V,T[V].Distence);
PrintPath(V, T);
printf("\n");
}
}
無權圖的最短路徑
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define INFINITY 65535
typedef int DistType;
typedef int Vertex;
typedef int WeightType;
typedef char DataType;
typedef int ElementType;
typedef struct QueueRecord
{
int Front;
int Rear;
int Capacity;
ElementType *Array;
}*Queue;
void InitQueue(Queue Q)
{
Q->Front = 0;
Q->Rear = 0;
}
Queue CreatQueue(int MaxSize)
{
Queue Q = (QueueRecord*)malloc(sizeof(QueueRecord));
Q->Capacity = MaxSize;
Q->Array = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
InitQueue(Q);
return Q;
}
int Enqueue(Queue Q, ElementType X)
{
if ((Q->Rear + 1) % Q->Capacity == Q->Front)
return -1;
Q->Rear = (Q->Rear + 1) % Q->Capacity;
Q->Array[Q->Rear] = X;
return 0;
}
ElementType Dequeue(Queue Q)
{
if (Q->Front == Q->Rear)
return -1;
Q->Front = (Q->Front + 1) % Q->Capacity;
return Q->Array[Q->Front];
}
typedef struct ENode
{
Vertex V1, V2;
}*Edge;
typedef struct AdjVNode
{
Vertex AdjV;
struct AdjVNode *Next;
}*PtrToAdjVNode;
typedef struct VNode
{
PtrToAdjVNode FirstEdge;
DataType Data;
}AdjList[MaxVertexNum];
typedef struct GNode
{
int Nv;
int Ne;
AdjList G;
}*LGraph;
LGraph CreateGraph(int VertexNum)
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V <= Graph->Nv; V++)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(AdjVNode));
NewNode->AdjV = E->V2;
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
}
LGraph BuildGraph()
{
LGraph Graph;
Edge E;
Vertex V;
int Nv, i;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &Graph->Ne);
if (Graph->Ne != 0)
{
E = (Edge)malloc(sizeof(ENode));
for (i = 0; i < Graph->Ne; i++)
{
scanf("%d %d", &E->V1, &E->V2);
InsertEdge(Graph, E);
}
}
return Graph;
}
typedef struct TableNode
{
int Known;
DistType Distence;
Vertex Path;
}Table;
void InitTable(Vertex Start, LGraph Graph, Table *T)
{
int i;
for (i = 0; i <= Graph->Nv; i++)
{
T[i].Distence = INFINITY;
T[i].Known = 0;
T[i].Path = 0;
}
T[Start].Distence = 0;
}
void Unweighted(Table *T, LGraph Graph, Vertex S)
{
Queue Q;
Q = CreatQueue(Graph->Nv);
InitQueue(Q);
Vertex V;
Enqueue(Q, S);
while (Q->Front != Q->Rear)
{
V = Dequeue(Q);
T[V].Known = 1;
PtrToAdjVNode W;
for (W = Graph->G[V].FirstEdge; W; W = W->Next)
{
if (T[W->AdjV].Distence == INFINITY)
{
T[W->AdjV].Distence = T[V].Distence + 1;
T[W->AdjV].Path = V;
Enqueue(Q, W->AdjV);
}
}
}
}
int main()
{
LGraph Graph = BuildGraph();
Table T[MaxVertexNum];
InitTable(3, Graph, T);
Unweighted(T, Graph, 3);
Vertex V;
for (V = 1; V <= Graph->Nv; V++)
printf("%d ", T[V].Distence);
}
拓撲排序
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
typedef int Vertex;
typedef int WeightType;
typedef char DataType;
typedef struct ENode
{
Vertex V1, V2;
WeightType Weight;
}*Edge;
typedef struct AdjVNode
{
Vertex AdjV;
WeightType Weight;
struct AdjVNode *Next;
}*PtrToAdjVNode;
typedef struct VNode
{
PtrToAdjVNode FirstEdge;
DataType Data;
}AdjList[MaxVertexNum];
typedef struct GNode
{
int Nv;
int Ne;
AdjList G;
}*LGraph;
LGraph CreateGraph(int VertexNum)
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; V++)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(AdjVNode));
NewNode->AdjV = E->V2;
NewNode->Weight = E->Weight;
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
}
LGraph BuildGraph()
{
LGraph Graph;
Edge E;
Vertex V;
int Nv, i;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &Graph->Ne);
if (Graph->Ne != 0)
{
E = (Edge)malloc(sizeof(ENode));
for (i = 0; i < Graph->Ne; i++)
{
scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
InsertEdge(Graph, E);
}
}
return Graph;
}
typedef int ElementType;
typedef struct QueueRecord
{
int Front;
int Rear;
int Capacity;
ElementType *Array;
}*Queue;
void InitQueue(Queue Q)
{
Q->Front = 0;
Q->Rear = 0;
}
Queue CreatQueue(int MaxSize)
{
Queue Q = (QueueRecord*)malloc(sizeof(QueueRecord));
Q->Capacity = MaxSize;
Q->Array = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
InitQueue(Q);
return Q;
}
int Enqueue(Queue Q, ElementType X)
{
if ((Q->Rear + 1) % Q->Capacity == Q->Front)
return -1;
Q->Rear = (Q->Rear + 1) % Q->Capacity;
Q->Array[Q->Rear] = X;
return 0;
}
ElementType Dequeue(Queue Q)
{
if (Q->Front == Q->Rear)
return -1;
Q->Front = (Q->Front + 1) % Q->Capacity;
return Q->Array[Q->Front];
}
bool TopSort( LGraph Graph, Vertex TopOrder[] )
{
int Indegree[MaxVertexNum], Index;
Vertex V;
PtrToAdjVNode W;
Queue Q = CreatQueue( Graph->Nv );
for (V=0; V<Graph->Nv; V++)
Indegree[V] = 0;
for (V=0; V<Graph->Nv; V++)
for (W=Graph->G[V].FirstEdge; W; W=W->Next)
Indegree[W->AdjV]++;
for (V=0; V<Graph->Nv; V++)
if ( Indegree[V]==0 )
Enqueue(Q, V);
Index = 0;
while( Q->Front != Q->Rear){
V = Dequeue(Q);
TopOrder[Index++] = V;
for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
if ( --Indegree[W->AdjV] == 0 )
Enqueue(Q, W->AdjV);
}
if ( Index != Graph->Nv )
return false;
else
return true;
}
int main()
{
LGraph Graph = BuildGraph();
Vertex Top[MaxVertexNum];
TopSort(Graph, Top);
for (Vertex V = 0; V < Graph->Nv; V++)
printf("%d ", Top[V]);
}