Question

1. Consider the weighted graph below: 11 15 E F G H 14 10 9 17 12 (a) Demonstrate Prims algorithm starting from vertex A. Write the edges in the order they were added to the minimum spanning tree. (b) Demonstrate Dijkstras algorithm on the graph, using vertex A as the source. Write the vertices in the order which they are marked and compute all distances at each step

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <limits.h>
#define V 8

int minKey(int key[], bool set[])
{
   int min = INT_MAX, index, i;
   for(i=0;i<V;i++)
   {
       if(set[i]==false && key[i]<min)
       {
           min = key[i];
           index = i;
       }
   }  
   return index;
}

void primPrint(int p[], int graph[][V], int sum)
{
   int i;
   char arr[8] = {'A','B','C','D','E','F','G','H'};
   printf("EDGES\n");
   printf("Source - Destination - Weight\n");
   printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
   for(i=1;i<V;i++)
   {
       printf(" %c - %c - %d \n", arr[p[i]], arr[i], graph[i][p[i]]);
       sum = sum + graph[i][p[i]];
   }
   printf("Total sum of weights are : %d\n\n",sum);
}

void prim(int graph[][V])
{
   int p[V];
   int key[V];
   bool pSet[V];
   int i,b;
   for(i=0;i<V;i++)
   {
       key[i] = INT_MAX;
       pSet[i] = false;
   }
   key[0] = 0;
   p[0] = -1;
   for(i=0;i<V-1;i++)
   {
       int a = minKey(key,pSet);
       set[a] = true;
       for(b=0;b<V;b++)
       {
           if(graph[a][b] && !pSet[b] && graph[a][b] < key[b])
           {
               p[b] = a;
               key[b] = graph[a][b];
           }
       }
   }
   primPrint(p, graph, 0);
}

void dijkstraPrint(int dist[])
{
   int i;
   char arr[8] = {'A','B','C','D','E','F','G','H'};
   printf("VERTICES\n");
   printf("From Source - Weight\n");
   printf("~~~~~~~~~~~~~~~~~~~~~~~~\n");
   printf(" %c - 0\n", arr[0]);
   for(i=1;i<V;i++)
       printf(" %c - %d \n", arr[i], dist[i]);
}

void dijkstra(int graph[][V], int source)
{
   int dist[V];
   bool dSet[V];
   int i,b;
   for(i=0;i<V;i++)
   {
       dist[i] = INT_MAX;
       dSet[i] = false;
   }
   dist[source] = 0;
   for(i=0;i<V-1;i++)
   {
       int a = minKey(dist,dSet);
       dSet[a] = true;
       for(b=0;b<V;b++)
       {
           if(graph[a][b] && !dSet[b] && dist[a]!=INT_MAX && dist[a]+graph[a][b] < dist[b])
               dist[b] = dist[a] + graph[a][b];
       }
   }
   dijkstraPrint(dist);
}

int main()
{
   int graph[V][V] = {{0, 5, 0, 0, 4, 0, 0, 0},
               {5, 0, 6, 0, 3, 14, 10, 0},
               {0, 6, 0, 1, 0, 0, 9, 17},
               {0, 0, 1, 0, 0, 0, 0, 12},
               {4, 3, 0, 0, 0, 11, 0, 0},
               {0, 14, 0, 0, 11, 0, 7, 0},
               {0, 10, 9, 0, 0, 7, 0, 15},
               {0, 0, 17, 12, 0, 0, 15, 0}};
   prim(graph);
   dijkstra(graph,0);
   return 0;
}

agilaa@agilaa: /Desktop/pro/C EX (M) (96%) 4)) 12:04 PM * agilaa@agilaa:/Desktop/pro/C$ g++ graph.c-o graph agilaa@agilaa:~/D

I have included both (a) and (b) in the same program as different functions.

Add a comment
Know the answer?
Add Answer to:
Consider the weighted graph below: Demonstrate Prim's algorithm starting from vertex A. Write the edges in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following weighted graph G:

    Consider the following weighted graph G: Use Prim's algorithm to find a minimal spanning tree T of this graph starting at the vertex s. You do not need to show every step of the algorithm, but to receive full credit you should: list the edges of T in the order in which they're added; redraw G and indicate which edges belong to T; compute the cost of T.

  • Consider the graph below. Use Prim's algorithm to find a minimal spanning tree of the graph rooted in vertex A.

    Consider the graph below. Use Prim's algorithm to find a minimal spanning tree of the graph rooted in vertex A. Note: enter your answer as a set of edges [E1, E2, ...) and write each edge as a pair of nodes between parentheses separate by a comma and one blank space e.g. (A,B) 

  • You're running Dijkstra's algorithm to find all shortest paths starting with vertex A in the graph...

    You're running Dijkstra's algorithm to find all shortest paths starting with vertex A in the graph below, but you pause after vertex E has been added to the solution (and the relaxation step for vertex E has been performed). Annotate the graph as follows: (1) label each node with its current dist value, (2) darken the edges that are part of the current spanning tree (i.e., the parent links), (3) draw a dotted circle around the "cloud'' of vertices that...

  • Run Prim (starting from vertex "f") and Kruskal algorithms on the graph below: 3 2 9...

    Run Prim (starting from vertex "f") and Kruskal algorithms on the graph below: 3 2 9 3 . (5 points) Prim's algorithm: draw a table that shows the vertices in the queue at each iteration, similar to example from the notes (2 points) Prim's algorithm: using the table from the first part, list the order in which edges are added to the tree (3 points) Kruskal's algorithm: list the order in which edges are added to the tree

  • Question 6 Let G be the weighted graph (a) Use Dijkstra's algorithm to find the shortest path from A to F. You can...

    Question 6 Let G be the weighted graph (a) Use Dijkstra's algorithm to find the shortest path from A to F. You can do all the work on a single diagram, but, to show that you have used the algorithm correctly, if an annotation needs updating do not erase itjust put a line through it and write the new annotation above that b) In what order are the vertices added to the tree? (c) Notice that the algorithm does not,...

  • Run Dijkstra's algorithm on the graph G below, where s is the source vertex.

    Run Dijkstra's algorithm on the graph G below, where s is the source vertex. Draw a table that shows the vertices in Q at each iteration. Write thed and I values of each vertex. Color the edges in the shortest-path tree, similar to the example from the notes. List the order in which vertices are added to S. Use the algorithm learned in class. 

  • Consider the following weighted undirected graph.

    Consider the following weighted undirected graph. (a) Explain why edge (B, D) is safe. In other words, give a cut where the edge is the cheapest edge crossing the cut. (b) We would like to run the Kruskal's algorithm on this graph. List the edges appearing in the Minimum Spanning Tree (MST) in the order they are added to the MST. For simplicity, you can refer to each edge as its weight. (c) 1We would like to run the Prim's algorithm on this...

  • Given the following weighted graph G. use Prim's algorithm to determine the Minimum-Cost Spanning Tree (MCST)...

    Given the following weighted graph G. use Prim's algorithm to determine the Minimum-Cost Spanning Tree (MCST) with node 1 as the "root". List the vertices in the order in which the algorithm adds them to the solution, along with the edge and its weight used to make the selection, one per line. Each line should look like this: add vertex y: edge = (x,y), weight = 5 When the algorithm ends there are, generally, edges left in the heap. List...

  • Using the following graph and Dijkstra's algorithm, calculate the shortest distance to each other vertex starting...

    Using the following graph and Dijkstra's algorithm, calculate the shortest distance to each other vertex starting from vertex A. Label all vertices with the total distance (from A). Indicate the order nodes are added to cloud. Draw a Minimum Spanning Tree for the graph. You should label all nodes in the tree, but you do not need to indicate edge weights or total distance. 2 D C L 7 6 2 7 2 A K B 4 7 4 1...

  • Algorithm Question 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in which the...

    Algorithm Question 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in which the edges are chosen, breaking ties by using vertices at the same length in alphabetic orde. 3 Ga 2 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT