Back to the 2024 paper

Module 3: Graph and Tree Algorithms

20247m

A graph with five nodes is represented using the adjacency list as follows:

A -> B (10) C (3)
B -> C (1) D (5)
C -> B (4) D (8) E (2)
D -> E (7)
E -> D (9)

Answer the following:
(i) Draw the graph for the above adjacency list.
(ii) Write the algorithm for the Breadth-First Traversal (BFS).
(iii) Show the BFS traversal of the graph with the starting node as A.

Worked SolutionAI Assisted

Graph from Adjacency List + BFS

Adjacency List (directed weighted, weight in brackets)

  • A → B(10), C(3)
  • B → C(1), D(5)
  • C → B(4), D(8), E(2)
  • D → E(7)
  • E → D(9)

(i) Graph Drawing (description)

Vertices A-E.
Edges: A→B (10), A→C (3), B→C (1), B→D (5), C→B (4), C→D (8), C→E (2), D→E (7), E→D (9). It's directed; no edge into A. Strongly connected component {B,C,D,E} partially.

If sketched: A at top, arrows to B (right) and C (middle); B to C (down-left, 1) and D; C to B (up), to D (right), to E (down); D⟷E bidirectional with weights 7/9.

(ii) BFS Algorithm (as in Q92)

BFS(G, s):
  for v in V: visited[v]=false, dist[v]=∞
  Q=queue(); visited[s]=true; dist[s]=0; enqueue(s)
  order=[]
  while Q not empty:
    u=dequeue()
    order.append(u)
    for v in Adj[u] (in list order):
      if not visited[v]:
        visited[v]=true; dist[v]=dist[u]+1; parent[v]=u; enqueue(v)

Time O(V+E)O(V+E), space O(V)O(V). BFS ignores weights (treats as unweighted for traversal order); for weighted shortest paths use Dijkstra.

(iii) BFS Traversal Starting A (assuming adjacency order as listed)

  • Init Q=[A], visited {A}, order []
  • Dequeue A → order [A]; neighbors B,C not visited → enqueue B,C → Q=[B,C]
  • Dequeue B → order [A,B]; neighbors C (already visited), D not visited → enqueue D → Q=[C,D]
  • Dequeue C → order [A,B,C]; neighbors B already, D already, E not visited → enqueue E → Q=[D,E]
  • Dequeue D → order [A,B,C,D]; neighbor E already visited (via C) → Q=[E]
  • Dequeue E → order [A,B,C,D,E]; neighbor D already visited → Q=[] done.

BFS order: A → B → C → D → E
Distances (edges): dist[A]=0, dist[B]=1, dist[C]=1, dist[D]=2 (via B), dist[E]=2 (via C).

Parent tree: parent[B]=A, parent[C]=A, parent[D]=B, parent[E]=C.

If adjacency order C before B, order would be A,C,B,E,D etc but standard given order yields A,B,C,D,E. BFS levels: L0 {A}, L1 {B,C}, L2 {D,E}.

Similar questions

Design & Analysis of AlgorithmsExplain back-tracking, DFS and BFS with help of small example. Differentiate in between backtracking and dynamic programming. Apply the backtracking algorithm to solve the three-colouring problem for the following graph using state space tree. Assume three colours red, green and blue. ![Graph for Q8](https://res.cloudinary.com/djkpavwmp/image/upload/v1765793825/portfolio_assets/qoy6nsycvjnin985kjnp.png)201914mDesign and Analysis of Algorithms Consider a TSP with 4 cities A, B, C, D and the following distance matrix: | | A | B | C | D | |---|---|---|---|---| | A | 0 | 10 | 15 | 20 | | B | 10 | 0 | 35 | 25 | | C | 15 | 35 | 0 | 30 | | D | 20 | 25 | 30 | 0 | Answer the following: (i) Use the branch-and-bound algorithm to find the shortest possible route that visits all cities exactly once and returns to the starting city. (ii) Show the branching steps and calculate lower bounds for each partial solution. (iii) Identify how the bounding helps prune the search tree.20247mDesign and Analysis of Algorithms Answer the following: (i) Compare and contrast the BFS and DFS traversal techniques in terms of their algorithmic approach, time complexity, data structures used, and typical applications. (ii) Give examples where one is preferred over the other.20247mDesign and Analysis of Algorithms Write the Pseudo-code for the Dijkstra's Algorithm. Further, find the single source (A) shortest path on the following graph: ![image](https://9lrt4yspyx.ufs.sh/f/z1z03jdN1p04c1Or1kxVDrIj0ikyQ82dzRXKGqChPbOmcuBt)20247m
Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.