0% found this document useful (0 votes)
11 views9 pages

UNIT 2 Problems

The document outlines various graph algorithms, including BFS, DFS, Kruskal's Algorithm for Minimum Spanning Tree, Dijkstra's Algorithm for shortest paths, and Ford-Fulkerson for maximum flow. It provides pseudocode for each algorithm, along with time and space complexity analysis, and step-by-step execution examples. The document concludes with specific results for each algorithm applied to given graphs, including total weights and maximum flow values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

UNIT 2 Problems

The document outlines various graph algorithms, including BFS, DFS, Kruskal's Algorithm for Minimum Spanning Tree, Dijkstra's Algorithm for shortest paths, and Ford-Fulkerson for maximum flow. It provides pseudocode for each algorithm, along with time and space complexity analysis, and step-by-step execution examples. The document concludes with specific results for each algorithm applied to given graphs, including total weights and maximum flow values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT 2 AND 3 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

while Q is not empty:


current = dequeue Q
print current
for each neighbor in Graph[current]:
if neighbor is not visited:
mark neighbor as visited
enqueue neighbor into Q

Pseudocode for DFS


DFS(Graph, start_vertex, visited):
mark start_vertex as visited
print start_vertex
for each neighbor in Graph[start_vertex]:
if neighbor is not visited:
DFS(Graph, neighbor, visited)
To initiate DFS:
visited = [False] number_of_vertices
DFS(Graph, start_vertex, visited)

Time and Space Complexity Comparison

Criterion BFS DFS

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

Finding shortest path in Topological sorting, solving puzzles


Best Use Case
unweighted graph like mazes

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

Edge List (sorted by weight)


Edge Weight

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

Kruskal's Algorithm Execution


1. g - h (1) → ✅ Add (no cycle)
2. g - f (2) → ✅ Add (no cycle)
3. a - b (4) → ✅ Add
4. i - g (6) → ✅ Add
5. c - d (7) → ✅ Add
6. h - i (7) → ❌ Skip (forms a cycle with g-h-i)
7. b - c (8) → ✅ Add
8. a - h (8) → ✅ Add
✅ Total edges in MST: 8 (done)

Minimum Spanning Tree Edges


 g - h (1)
 g - f (2)
 a - b (4)
 i - g (6)
 c - d (7)
 b - c (8)
 a - h (8)
 ✅ MST complete

Total Weight of MST


= 1 + 2 + 4 + 6 + 7 + 8 + 8 = 36

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.

✅ Dijkstra’s Algorithm – Pseudocode


Dijkstra(Graph, source):
dist[] = array of size V, initialized to ∞
dist[source] = 0
priority_queue = empty min-heap based on dist[]

insert (0, source) into priority_queue

while priority_queue is not empty:


(current_dist, u) = extract_min(priority_queue)
for each neighbor v of u:
if dist[u] + weight(u, v) < dist[v]:
dist[v] = dist[u] + weight(u, v)
insert (dist[v], v) into priority_queue

Step-by-Step Execution on the Graph


Vertices: 0 to 8
Source: 0
Edge List (with weights):
(0-1, 4), (0-7, 8)
(1-2, 8), (1-7, 11)
(2-3, 7), (2-8, 2), (2-5, 4)
(3-4, 9), (3-5, 14)
(4-5, 10)
(5-6, 2)
(6-7, 1), (6-8, 6)
(7-8, 7)

🧠 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, ∞]

3 7 [0, 4, 12, ∞, ∞, ∞, ∞, 8, 15]

4 2 [0, 4, 12, 19, ∞, 16, ∞, 8, 14]

5 8 [0, 4, 12, 19, ∞, 16, 20, 8, 14]

6 5 [0, 4, 12, 19, ∞, 16, 18, 8, 14]

7 6 [0, 4, 12, 19, ∞, 16, 18, 8, 14]

8 3 [0, 4, 12, 19, 28, 16, 18, 8, 14]

9 4 [0, 4, 12, 19, 26, 16, 18, 8, 14]

✅ Final Shortest Path Distances from Vertex 0:


Vertex Distance from 0

0 0

1 4

2 12

3 19

4 26

5 16

6 18

7 8

8 14

🧮 Time and Space Complexity

Metric Value

Time Complexity O((V + E) log V) using min-heap


Metric Value

Space Complexity O(V) for distances, O(E) for adjacency list

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.

Step 1: Initialize Residual Graph


The residual graph is initially the same as the given network with all flows set to 0.

Step 2: Find Augmenting Paths


We will use the Depth-First Search (DFS) method to find augmenting paths in the
residual graph.

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).

Since no more augmenting paths can be found from S to T, the algorithm


terminates.EG
Final Flows:
- S → A: 0
- S → B: 1
- S → C: 0
- A → D: 0
- B → D: 1
- C → D: 0
- D → T: 1

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.

You might also like