/* * Code example for CP264 Data Structures II * adopted from source: https://www.thecrazyprogrammer.com/2014/03/dijkstra-algorithm-for-finding-shortest-path-of-a-graph.html * this program is not an efficient implementation of Dijkstra algorithm. * HBF */ #include #include #define INFINITY 9999 void dijkstra_spt(int *g, int n, int start, int spt[]); void display_spt(int spt[], int n); int shortest_path(int *g, int t[], int n, int source, int destination); int main(){ int n = 5; int source = 0; // adjacency matrix of undirected weighted graph int g[] = { 0, 10, 0, 30, 100, 10, 0, 50, 0, 0, 0, 50, 0, 20, 10, 30, 20, 0, 0, 60, 100, 0, 10, 60, 0 }; // array to represent a tree with edge (i, t[i]) when i != t[i], root i if i = t[i] int t[n]; // compute the shortest path tree, and output to t[n] dijkstra_spt(g, n, source, t); // display edge list of tree t[] display_spt(t, n); int i; for (i=0; i0; j--) { printf("{%d, %d}", temp[j], temp[j-1]); if (j>1) printf(", "); } } /* Shortest path tree: root: 0, edge-list: {0, 1},{1, 2},{0, 3},{2, 4} shortest-path(0, 0): length: 0, edge-list: shortest-path(0, 1): length: 10, edge-list: {0, 1} shortest-path(0, 2): length: 60, edge-list: {0, 1}, {1, 2} shortest-path(0, 3): length: 30, edge-list: {0, 3} shortest-path(0, 4): length: 70, edge-list: {0, 1}, {1, 2}, {2, 4} */