Graph_Algorithms_Solutions
Graph_Algorithms_Solutions
-------------------------------------
BFS explores all neighbors of a vertex before moving deeper into the graph. It uses a queue to keep
Start at vertex A. Use a queue to keep track of vertices to visit: Queue = [A]. Maintain a visited list to
1. Visit A: Dequeue A -> Queue = []. Add B and C (neighbors of A) to the queue -> Queue = [B, C].
2. Visit B: Dequeue B -> Queue = [C]. Add D (neighbor of B) to the queue -> Queue = [C, D]. Mark B
3. Visit C: Dequeue C -> Queue = [D]. Add E (neighbor of C) to the queue -> Queue = [D, E]. Mark
4. Visit D: Dequeue D -> Queue = [E]. Add F (neighbor of D) to the queue -> Queue = [E, F]. Mark D
5. Visit E: Dequeue E -> Queue = [F]. No neighbors to add. Mark E as visited -> Visited = {A, B, C,
D, E}.
6. Visit F: Dequeue F -> Queue = []. No neighbors to add. Mark F as visited -> Visited = {A, B, C, D,
E, F}.
-------------------------------------
DFS explores as far as possible along each branch before backtracking. It uses a stack (or
Start at vertex A. Use a stack to keep track of vertices to visit: Stack = [A]. Maintain a visited list to
1. Visit A: Pop A -> Stack = []. Add B and C (neighbors of A) to the stack -> Stack = [C, B]. Mark A
2. Visit B: Pop B -> Stack = [C]. Add D (neighbor of B) to the stack -> Stack = [C, D]. Mark B as
3. Visit D: Pop D -> Stack = [C]. Add F (neighbor of D) to the stack -> Stack = [C, F]. Mark D as
4. Visit F: Pop F -> Stack = [C]. No neighbors to add. Mark F as visited -> Visited = {A, B, D, F}.
5. Visit C: Pop C -> Stack = []. Add E (neighbor of C) to the stack -> Stack = [E]. Mark C as visited ->
6. Visit E: Pop E -> Stack = []. No neighbors to add. Mark E as visited -> Visited = {A, B, D, F, C, E}.
-------------------------------------
Question: Find the MST for the graph with the following edges: {A-B: 1, A-C: 4, B-C: 2, B-D: 5, C-D:
3}.
Initialize MST = {} and MST weight = 0. Add edges in order, checking for cycles.
1. Add edge A-B (1): No cycle is formed. Add A-B to MST. MST = {A-B}, MST weight = 1.
2. Add edge B-C (2): No cycle is formed. Add B-C to MST. MST = {A-B, B-C}, MST weight = 3.
3. Add edge C-D (3): No cycle is formed. Add C-D to MST. MST = {A-B, B-C, C-D}, MST weight = 6.
4. Skip edges A-C (4) and B-D (5): Adding these edges forms cycles.
4. Dijkstra's Algorithm
-------------------------------------
Question: Find the shortest path from vertex A to all other vertices in the graph: Edges: {A->B (1),
1. Process vertex A: Dequeue (0, A). Explore neighbors of A: Update distance[B] = 1, distance[C] =
2. Process vertex B: Dequeue (1, B). Explore neighbors of B: Update distance[C] = 3, distance[D] =
3. Process vertex C: Dequeue (3, C). Explore neighbors of C: Update distance[D] = 6. Queue: [(6,
D)].
-------------------------------------
Question: Solve for weights {1, 2, 4, 2}, values {10, 20, 40, 25}, and capacity = 5.
Define DP[i][w] as the maximum value we can get using the first i items and weight limit w.
1. For item 1 (weight 1, value 10): Fill row based on whether weight w >= 1.