UNIT 2 Problems
UNIT 2 Problems
5. Given the graph in the image, which consists of vertices {0, 1, 2, 3, 4} connected as
follows:
0 is connected to 1 and 2
1 is connected to 0 and 3
2 is connected to 0 and 4
3 is connected to 1 and 4
4 is connected to 2 and 3
Pseudocode for BFS
BFS(Graph, start_vertex):
create a queue Q
mark start_vertex as visited
enqueue start_vertex into Q
Time
O(V + E) O(V + E)
Complexity
Space
O(V) for visited + O(V) for queue O(V) for visited + O(V) for call stack
Complexity
Where:
V is the number of vertices
E is the number of edges
6. To find the Minimum Spanning Tree (MST) of the given weighted graph using
Kruskal’s Algorithm, follow these steps:
Kruskal’s Algorithm Pseudocode
1. Create a list of all edges in the graph, sorted by increasing weight.
2. Initialize an empty MST.
3. Initialize a disjoint-set (union-find) for all vertices.
4. For each edge in the sorted edge list:
a. If the edge connects two different sets (i.e., no cycle is formed):
- Add the edge to the MST.
- Union the sets.
5. Repeat until MST has (V - 1) edges.
Step-by-step Execution on the Graph
Vertices: a, b, c, d, e, f, g, h, i
Number of vertices (V): 9
MST will have (V - 1) = 8 edges
g-h 1
g-f 2
a-b 4
i-g 6
c-d 7
h-i 7
b-c 8
a-h 8
d-e 9
f - e 10
h - b 11
d - f 14
Result
Minimum Spanning Tree (MST) Edges using Kruskal’s Algorithm:
{ (g, h), (g, f), (a, b), (i, g), (c, d), (b, c), (a, h), (c, d) }
Total Weight = 36
7. To find the shortest paths from the source vertex 0 to all other vertices in the
given weighted graph, we use Dijkstra's Algorithm.
🧠 Initialize:
dist[] = [0, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞]
prev[] = [null,...]
🔁 Iteration Table (Selected Steps):
Step Vertex Updated Distance Array
1 0 [0, 4, ∞, ∞, ∞, ∞, ∞, 8, ∞]
2 1 [0, 4, 12, ∞, ∞, ∞, ∞, 8, ∞]
0 0
1 4
2 12
3 19
4 26
5 16
6 18
7 8
8 14
Metric Value
8. To find the maximum possible flow in the given network using the Ford-Fulkerson
algorithm, we will follow these steps:
Given Network:
- Source (S): Connected to nodes A, B, and C with capacities 8, 9, and 3 respectively.
- Node A: Connected to node D with capacity 2.
- Node B: Connected to node D with capacity 4.
- Node C: Connected to node D with capacity 5.
- Node D: Connected to the sink (T) with capacity 1.
Augmenting Path 1: S → B → D → T
- Bottleneck capacity: min(9, 4, 1) = 1
- Update flows:
- S → B: 0 → 1
- B → D: 0 → 1
- D → T: 0 → 1
- Residual capacities:
- S → B: 9 → 8
- B → D: 4 → 3
- D → T: 1 → 0
Augmenting Path 2: S → A → D → T
- Bottleneck capacity: min(8, 2, 0) → No flow possible (D → T is saturated).
Augmenting Path 3: S → C → D → T
- Bottleneck capacity: min(3, 5, 0) → No flow possible (D → T is saturated).
Augmenting Path 4: S → A → D → B → D → T
- Not a valid path (no backward edges with flow in this step).
Maximum Flow:
The maximum flow is the sum of flows leaving the source or entering the sink:
- Maximum flow = 1 (S → B → D → T).
Answer:
The maximum possible flow in the given network is 1.
Note: The given network has a bottleneck at the edge D → T with capacity 1, which
limits the maximum flow. Even though the source can send more flow through other
paths, the sink can only receive a flow of 1.