_datastructure_c_impl:dijkstra algorithm to find the shortest path

Source: Internet
Author: User

_datastructure_c_impl:dijkstra#include<stdio.h> #include <stdlib.h> #include <string.h>typedef Char vertextype[4];typedef char infoptr;typedef int vrtype; #define INFINITY 100000//defines an infinitely large value # define MaxSize 50// Maximum vertex number typedef int PATHMATRIX[MAXSIZE][MAXSIZE];//defines a two-dimensional array that holds the shortest path, typedef int SHORTPATHLENGTH[MAXSIZE];// Defines an array that holds the shortest distance from the vertex v0 to the vertex v typedef enum{dg,dn,ug,un}graphkind;typedef STRUCT{VRTYPE adj;//for an unauthorized graph, 1 for adjacent, 0 for non-adjacent, and for weighted graphs, Storage weights infoptr *info;//correlation information with arcs or edges}arcnode,adjmatrix[maxsize][maxsize];//diagram type definition typedef struct{vertextype VEX[MAXSIZE] ;//For storing vertex adjmatrix arc;//adjacency matrix, storing edge or arc information int vexnum,arcnum;//vertex number and Edge (ARC) number Graphkind kind;//diagram type}mgraph;//add a storage network row, Type definitions for columns and weights typedef struct{int row;int col;int Weight;} gnode;//using adjacency matrix notation to create a mesh nvoid creategraph (mgraph *n,gnode *value,int vnum,int arcnum,vertextype *ch) {int I,j,k,w;char s[ MaxSize]; Vertextype V1,v2; n->vexnum=vnum; N->arcnum=arcnum;for (i=0;i<vnum;i++) strcpy (N->vex[i],ch[i]);//Initialize adjacency matrix for (i=0;i<n->vexnum;i++) for (j=0;j<n->vexnum;j++) {n->arc[i][j].adj=infinity; n->arc[i][j].info=null;//Arc information is initialized to null}for (k=0;k<arcnum;k++) {i=value[k].row;j=value[k].col; N->arc[i][j].adj=value[k].weight;} The type of the n->kind=dn;//graph is the}//output adjacency matrix storage representation of the graph Nvoid displaygraph (mgraph N) {int i,j;printf ("The forward mesh has%d vertex%d arcs, the vertices are sequentially:", N.vexnum,n.arcnum); for (i=0;i<n.vexnum;++i)//Output mesh vertex printf ("%s", N.vex[i]);p rintf ("\ n" \ n "\ n");//output Net N arc printf ("Ordinal i="), for (i=0;i<n.vexnum;i++) printf ("%8d", i);p rintf ("\ n"), for (i=0;i<n.vexnum;i++) {printf ("%8d", I); (j=0;j<n.vexnum;j++) printf ("%8d", N.arc[i][j].adj);p rintf ("\ n"); }}/* uses the Dijkstra algorithm to find the v0 vertex of the direction net N to the shortest path of the remaining vertex v P[v] and the weighted length d[v]*//*final[v] is 1 for v∈s, that is, the shortest path from V0 to V has been calculated */void Dijkstra (mgraph N, int V0,pathmatrix path,shortpathlength dist) {int v,w,i,k,min;int final[maxsize];//record v0 to the shortest path to the vertex has been calculated for (v=0;v< n.vexnum;v++) {//Array dist stores v0 to v minimum distance, initialized to V0 to V arc distance final[v]=0;dist[v]=n.arc[v0][v].adj;for (w=0;w<n.vexnum;w++) Path[v][w]=0;if (dist[v]<infinity) {//If there is a direct path from V0 to V, the array of paths is initialized Path[v][v0]=1;path[v][v]=1;}} dist[v0]=The path of 0;//v0 to V0 merges the 0final[v0]=1;//v0 vertex into the collection s/* the shortest path from V0 to the remaining g.vexnum-1 vertices, and merges the vertex into the collection s*/for (i=1;i<n.vexnum;i++) {min= Infinity;for (w=0;w<n.vexnum;w++) if (!final[w]&&dist[w]<min) {//Find the closest vertex to V0 in a vertex that is not part of the collection S v=w;// Assign it to V at the nearest vertex W to V0, and its distance to minmin=dist[w];}  Final[v]=1; The V is incorporated into the set SFOR (w=0;w<n.vexnum;w++)//The shortest path length and the shortest path array that update v0 to vertices that do not belong to the set S, using the newly incorporated vertices of the set S (!final[w]&&min< infinity&&n.arc[v][w].adj<infinity&& (Min+n.arc[v][w].adj<dist[w])) {dist[w]=min+N.arc[v][ W].adj;for (k=0;k<n.vexnum;k++) path[w][k]=path[v][k];p ath[w][w]=1;}} void Main () {int i,j,vnum=6,arcnum=9; Mgraph N; Gnode value[]={{0,1,30},{0,2,60},{0,4,150},{0,5,40},{1,2,40},{1,3,100},{2,3,50},{3,4,30},{4,5,10}}; Vertextype ch[]={"V0", "V1", "V2", "V3", "V4", "V5"}; Pathmatrix path;/* uses a two-dimensional array to store the shortest path of the vertex */shortpathlength dist;/* with a one-dimensional array to hold the shortest path length */creategraph (&n,value,vnum,arcnum, CH); /* Create a N*/displaygraph (N),/* Output to N*/dijkstra (n,0,path,dist);p rintf (the shortest path length of "%s to each vertex is: \ n", n.vex[0]); for (i=0;i< N.vexnum;++i) if (i!=0) printf ("%s-%s:%D\n ", N.vex[0],n.vex[i],dist[i]); System (" Pause ");} 

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

_datastructure_c_impl:dijkstra algorithm to find the shortest path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.