图的邻接矩阵表示方式——有权图的最短路径Dijkstra算法

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 #define OK 1
  5 #define NO 0
  6 #define TRUE 1
  7 #define FALSE 0
  8 #define ERROR -1 
  9 
 10 #define MaxVerTexNum 100
 11 #define INFINITY 65535
 12 
 13 
 14 
 15 
 16 typedef int ElementType;
 17 typedef int Status;
 18 typedef int WeightType;
 19 typedef int Vertex;
 20 typedef char DataType;
 21 
 22 typedef int Position;
 23 typedef struct QNode{
 24     ElementType *Data;
 25     Position Front, Rear;
 26     int MaxSize;  
 27 }QNode,*Queue;
 28 
 29 
 30 
 31 Status Visited[MaxVerTexNum];
 32 
 33 typedef  struct  ENode{
 34     Vertex V1,V2;     //有向边<v1,V2>
 35     WeightType Weight;//权重
 36 }ENode,*PtrToENode ;
 37 
 38 typedef PtrToENode Edge;
 39 
 40 Status IsFull( Queue Q );
 41 
 42 typedef struct GNode{
 43     int Nv;//顶点数
 44     int Ne;//边数
 45     WeightType G[MaxVerTexNum][MaxVerTexNum];//邻接矩阵
 46     DataType Data[MaxVerTexNum];//save data of the Vertex;
 47 }GNode,*PtrToGNode;
 48 
 49 typedef PtrToGNode MGraph;
 50 
 51 MGraph CreateGraph(int VertexNum);//Create a Graph with VertexNum Vertex but without an ege;
 52 
 53 void InsertEdge(MGraph Graph,Edge E);
 54 
 55 MGraph BuildGraph();
 56 
 57 Status IsEdge(MGraph Graph,Vertex V,Vertex W);//检查<V,W>是不是图中的一条边,即W是不是V的邻接点
 58 
 59 
 60 
 61 Vertex FindMinDist(MGraph Graph,int dist[],int collected[]);
 62 
 63 Status Dijkstra(MGraph Graph,int dist[],int path[],Vertex S);
 64 
 65 int main(int argc,char** argv)
 66 {
 67     MGraph Graph;
 68     Graph=BuildGraph();
 69     int dist[MaxVerTexNum];
 70     int path[MaxVerTexNum];
 71     int S=1;
 72     int i,j;
 73     int sum;
 74     for(i=0;i<MaxVerTexNum;i++)
 75     {
 76         dist[i]=INFINITY;
 77         path[i]=-1;
 78     }
 79 
 80 
 81     
 82     Dijkstra(Graph,dist,path,S);
 83      for(j=0;j<Graph->Nv;j++){     //出现死循环要多调试
 84         i=j;
 85         sum=0;
 86     while(i!=-1){
 87         
 88         printf("%d",i);
 89         i=path[i];
 90         if(i!=-1){
 91             printf("<--");
 92             sum++;
 93         }
 94         
 95     }
 96     printf("\n");
 97     printf("从%d到%d的无权图最短路径为%d\n",S,j,sum);
 98     printf("\n");
 99     }
100 
101     return 0;
102 }
103 
104 MGraph CreateGraph(int VertexNum){
105     Vertex V,W;
106     MGraph Graph;
107     Graph=(MGraph)malloc(sizeof(GNode));
108     Graph->Nv=VertexNum;
109     Graph->Ne=0;//
110     for(V=0;V<Graph->Nv;V++)
111         for(W=0;W<Graph->Nv;W++)
112             Graph->G[V][W]=INFINITY;
113         return Graph;
114 }
115 
116 void InsertEdge(MGraph Graph,Edge E){
117     Graph->G[E->V1][E->V2]=E->Weight;
118     //若是无向图还要插入
119     Graph->G[E->V2][E->V1]=E->Weight;
120 }
121 
122 MGraph BuildGraph(){
123     MGraph Graph;
124     Edge E;
125     Vertex V;
126     int Nv,i;
127     printf("输入结点的个数\n");
128     scanf("%d",&Nv);//the number of vertex
129     Graph=CreateGraph(Nv);//initate graph with Nv vertexs  !!return 回去要赋值给Graph;
130     //getchar();
131     printf("输入弧的个数\n");
132     scanf("%d",&(Graph->Ne));//read the number of ege
133     if(Graph->Ne!=0)
134     {
135         E=(Edge)malloc(sizeof(ENode));
136         for(i=0;i<Graph->Ne;i++){
137             // getchar();
138             printf("输入弧的信息V1 V2 Weight\n");
139             scanf("%d %d %d",&(E->V1),&(E->V2),&(E->Weight));
140             InsertEdge(Graph,E);
141         }
142     }
143     // for(V=0;V<Graph->Nv;V++)
144     //     scanf("%c",&(Graph->Data[V]));
145     return Graph;
146 
147 }
148 Status IsEdge(MGraph Graph,Vertex V,Vertex W){
149     return Graph->G[V][W]<INFINITY?TRUE:FALSE;
150 }
151 
152 
153 Vertex FindMinDist(MGraph Graph,int dist[],int collected[]){   //返回未被收录顶点中dist最小者
154     Vertex MinV,V;  //其中MinV用来标记返回的顶点序号,V就是作为顶点的序号
155     int MinDist=INFINITY;//MinDist用来找到所有未被访问的顶点中Dist最小的那个
156     for(V=0;V<Graph->Nv;V++)
157     {
158         if(collected[V]==FALSE&&dist[V]<MinDist){
159             /* 若V未被收录,且dist[V]更小 */
160             MinDist=dist[V];//更新最小距离
161             MinV=V;
162         }
163     }
164         if(MinDist<INFINITY)//找到最小的dist
165             return MinV;
166         else 
167             return ERROR;
168 
169 }
170 
171 Status Dijkstra(MGraph Graph,int dist[],int path[],Vertex S){
172     int collected[MaxVerTexNum];
173     Vertex V,W;
174     //初始化,默认邻接矩阵中不存在的边用INFINITY表示
175     /*对dist的初始化非常的重要,因为第一个点必须从S点开始,因此要把它的dist设置成0,但默认值是INFINITY所以要改正
176     在初始化的时候也一定要满足其path[S]=-1的条件 */
177     for(V=0;V<Graph->Nv;V++)
178     {
179         dist[V]=Graph->G[S][V]; //初始化为邻接矩阵的大小
180         if(S==V)
181             dist[V]=0;//!!!!
182         if(dist[V]<INFINITY&&dist[V]!=0)  //!!!!
183             path[V]=S;//标记路线的前一个值为S
184         else
185             path[V]=-1;//前一个结点不存在
186         collected[V]=FALSE;    //初始化标记矩阵
187     }
188     while(1){
189         //V=为被收录的顶点中dist最小的那个
190         V=FindMinDist(Graph,dist,collected);
191         if(V==ERROR)
192             break;//如果这样的点不存在,表明剩下的点没有与已知的点连接或者所有的点都已经被连接标记
193         collected[V]=TRUE;//收录V
194         for(W=0;W<Graph->Nv;W++) //对图中每个顶点
195             if(collected[W]==FALSE&&Graph->G[V][W]<INFINITY){
196                 if(Graph->G[V][W]<0)//若有负边
197                     return ERROR;
198                 if(dist[V]+Graph->G[V][W]<dist[W])
199                     dist[W]=dist[V]+Graph->G[V][W];
200                     path[W]=V;
201             }
202 
203     
204     
205     
206     }
207 
208 
209 }

转载于:https://www.cnblogs.com/lovecodepql/p/7780882.html

### 回答1: Dijkstra算法是一种用于有向最短路径算法。使用邻接矩阵实现Dijkstra算法的步骤如下: 1. 初始化:将起点到各个顶点的距离初始化为无穷大,将起点到自身的距离初始化为0。 2. 选择起点:从未处理的顶点中选择距离起点最近的顶点作为当前顶点。 3. 更新距离:对于当前顶点的所有邻接点,如果从起点到当前顶点再到邻接点的距离小于起点到邻接点的距离,则更新起点到邻接点的距离。 4. 标记已处理:将当前顶点标记为已处理。 5. 重复步骤2-4,直到所有顶点都被标记为已处理或者没有可达的顶点。 6. 输出结果:输出起点到各个顶点的最短距离。 邻接矩阵实现Dijkstra算法的时间复杂度为O(n^2),其中n为顶点数。 ### 回答2: 邻接矩阵是一种表示结构的方法,具体而言,若G有n个顶点,则邻接矩阵是一个n x n矩阵,其中每个元素表示两个顶点之间的边。在有向中,若从顶点i到顶点j有一条有向边,则邻接矩阵中第i行第j列的元素为1,否则为0。 Dijkstra算法是一种经典的最短路径算法,它可以在带有向中求出从一个起点到所有其他点的最短路径Dijkstra算法的基本思想是,维护一个待访问的顶点集合V,初始时只包含起点。对于V中的每个顶点,记录起点到该顶点的最短路径长度以及已确定最短路径的所有顶点。在每一轮中,选择V中距离起点最近的顶点u,并将其加入已确定最短路径的集合S。然后,对于u的所有邻居v,如果通过u可以得到比当前路径更短的路径,则更新v的最短路径信息。重复上述过程,直到所有顶点都已被加入S为止。 使用邻接矩阵实现Dijkstra算法的基本思路是,定义两个数组dis和visited,分别记录起点到各个顶点的距离和该顶点是否已被访问。初始时,将起点的dis值设为0,visited值设为true。然后,遍历与起点相邻的顶点,并更新它们的dis值。在每一轮中,选择距离起点最近的未被访问的顶点u,并将visited[u]设为true。然后,依次遍历u的所有邻居v,如果通过u可以得到比当前路径更短的路径,则更新v的dis值。最后,重复进行n次循环,直到所有顶点的dis值都已确定。在实际实现中,可以借助优先队列等数据结构来加速算法的执行。 总的来说,使用邻接矩阵实现Dijkstra算法是一种简单有效的方法,它可以处理中等规模的带有向。然而,在处理大规模时,邻接矩阵的空间复杂度和时间复杂度都较高,需要使用其他更高效的算法和数据结构来解决。 ### 回答3: Dijkstra算法是一种常见的最短路径算法,在有向中可以使用邻接矩阵来实现。邻接矩阵是一种二维数组,其中的元素表示两点之间的连接关系,如果两个点之间有连接,那么数组元素的值为连接的重,否则为无穷大。 首先,需要定义一个类,从而可以将邻接矩阵封装在内部,同时提供相应的函数来实现Dijkstra算法。如下所示: ``` class Graph: def __init__(self, V): self.V = V self.graph = [[0 for column in range(V)] for row in range(V)] def dijkstra(self, src): dist = [float('inf')] * self.V dist[src] = 0 sptSet = [False] * self.V for i in range(self.V): u = self.minDistance(dist, sptSet) sptSet[u] = True for v in range(self.V): if self.graph[u][v] > 0 and sptSet[v] == False and \ dist[v] > dist[u] + self.graph[u][v]: dist[v] = dist[u] + self.graph[u][v] self.printSolution(dist) def minDistance(self, dist, sptSet): min = float('inf') min_index = -1 for v in range(self.V): if dist[v] < min and sptSet[v] == False: min = dist[v] min_index = v return min_index def printSolution(self, dist): for node in range(self.V): print(f"Node {node} distance from source: {dist[node]}") ``` 在主函数中,首先需要创建一个对象,然后按照邻接矩阵方式设置中边的重。接着,可以调用Dijkstra算法来计算从源节点到其他所有节点的最短路径长度。最后,输出所有节点的距离值。 代码示例如下: ``` # create graph V = 9 g = Graph(V) # set vertices and edges g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0], [4, 0, 8, 0, 0, 0, 0, 11, 0], [0, 8, 0, 7, 0, 4, 0, 0, 2], [0, 0, 7, 0, 9, 14, 0, 0, 0], [0, 0, 0, 9, 0, 10, 0, 0, 0], [0, 0, 4, 14, 10, 0, 2, 0, 0], [0, 0, 0, 0, 0, 2, 0, 1, 6], [8, 11, 0, 0, 0, 0, 1, 0, 7], [0, 0, 2, 0, 0, 0, 6, 7, 0]] # calculate shortest path g.dijkstra(0) ``` 以上就是使用邻接矩阵实现有向最短路径Dijkstra算法的简要介绍。该算法常用于寻找两点之间的最短路径、计算网络中消息传递的时间等问题。当然,如果有向中存在负边,则需要使用Bellman-Ford算法来解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值