2081 12

CSC211 ยท 2081 ยท 5 marks

Write short notes on: a. Breadth First traversal of graph b. TOH

Write short notes on: a. Breadth First traversal of graph b. TOH [5]

Data Structures and AlgorithmsChapter 8: Trees and GraphsDefinition and Representation of Graphs, Graph Traversal, Mi

Short Notes


a. Breadth First Search (BFS) Traversal of Graph

Definition: BFS is one of the simplest methods of graph searching/traversal. It explores a graph level by level, visiting all neighbors of a vertex before moving to the next level.

Algorithm / Steps:

  1. Choose some vertex arbitrarily as the root (starting vertex).
  2. Visit the root and add all vertices and edges that are incident on the root. These newly added vertices form Level 1 of the BFS tree.
  3. From the set of vertices at Level 1, find other vertices that are connected by edges to Level 1 vertices. These form Level 2.
  4. Repeat the above step until all vertices are visited/added.
  5. A queue data structure is used to keep track of vertices to be visited next.

Example:

Graph:
    A
   / \
  B   C
 / \
D   E

BFS Order: A -> B -> C -> D -> E

Key Features:

  • Uses a Queue (FIFO)
  • Visits vertices in order of their distance from the source
  • Guarantees the shortest path in an unweighted graph

b. Tower of Hanoi (TOH)

Definition: Tower of Hanoi is a classic recursive problem that involves moving n disks from a source peg to a destination peg using an auxiliary (helper) peg, following specific rules.

Rules:

  1. Only one disk can be moved at a time.
  2. A disk can only be placed on top of a larger disk or on an empty peg.
  3. Only the top disk of any peg can be moved.

Recursive Algorithm: To move n disks from Source (S) to Destination (D) using Auxiliary (A):

  1. Move n-1 disks from S to A (using D as auxiliary)
  2. Move the nth (largest) disk from S to D
  3. Move n-1 disks from A to D (using S as auxiliary)

Example (n = 3 disks, S -> D using A):

StepMove
1S -> D
2S -> A
3D -> A
4S -> D
5A -> S
6A -> D
7S -> D

Total moves = 2^n - 1 = 2^3 - 1 = 7 moves

Key Features:

  • Classic example of recursion
  • Minimum number of moves required = 2^n - 1
  • Time Complexity: O(2^n)

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.