Advanced Graph Algorithm Problems -
15 Mark Answers
Q1. Apply Prim’s Algorithm to the Given Graph
Prim's Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a
connected weighted graph. The MST connects all vertices in the graph with the minimum
total edge weight and without forming any cycles.
Algorithm Steps:
1. Start from any node (usually the one with the smallest edge).
2. Add the node to the MST.
3. Select the minimum weight edge that connects a vertex in the MST to a vertex outside the
MST.
4. Repeat step 3 until all vertices are included in the MST.
Given Graph Vertices: A, B, C, D, E, F
Steps:
- Start from A
- A → C (3)
- C → D (1)
- D → F (4)
- C → E (5)
- B → E (2)
MST Edges: (A-C), (C-D), (D-F), (C-E), (E-B)
Total Weight = 3 + 1 + 4 + 5 + 2 = 15
Q2. Hungarian Algorithm for Assignment Problem
The Hungarian Algorithm is used to solve the assignment problem which involves assigning
tasks to agents at minimal cost.
Steps:
1. Subtract the row minimum from each row.
2. Subtract the column minimum from each column.
3. Cover all zeros using the minimum number of lines.
4. If lines < n, adjust matrix and repeat.
5. Find the optimal assignment.
Given a 4x4 cost matrix, perform reductions and assignments accordingly.
Final Output: Optimal task assignment with minimum total cost.
Q3. Floyd Warshall’s Algorithm for All-Pairs Shortest Path
Floyd-Warshall Algorithm computes the shortest paths between all pairs of vertices in a
weighted graph.
Steps:
1. Let dist[i][j] = weight of edge (i, j), or ∞ if no edge exists.
2. For each vertex k, update:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
Given Distance Matrix is used to initialize `dist`.
Final Output: A matrix showing shortest path distances between every pair of vertices.
Q4. Apply Prim’s and Kruskal’s Algorithm for MST
Kruskal’s Algorithm:
- Sort all edges in non-decreasing order of their weight.
- Add edges to MST using Union-Find, avoiding cycles.
- Stop when MST includes (V-1) edges.
Prim’s Algorithm:
- Uses a priority queue and always selects the edge with the smallest weight that connects
to the tree.
Both yield the same MST for connected, undirected graphs but operate differently.
Final Output: Set of MST edges and their total weight.
Q5. Apply Kruskal’s Algorithm on Given Graph
Steps:
1. Sort edges by weight: (C-F), (C-D), (B-C), (A-B), (E-F), ...
2. Use Union-Find to add edges to the MST avoiding cycles.
3. Add until MST contains (V-1) = 5 edges.
MST Edges: (C-F), (C-D), (B-C), (A-B), (E-F)
Total Weight = Sum of included edge weights.
Q6. Apply Kruskal’s Algorithm on Another Graph
Follow same procedure as Q5.
- List all edges with weights.
- Sort them.
- Add edges while checking for cycles using Disjoint Set (Union-Find).
- Final MST includes (V-1) edges.
Final Output: MST edges + total weight.