9 Trees And Graph Algorithms

Discrete Structure ยท Unit 9

Trees and Graph Algorithms

Exam-focused notes for Trees and Graph Algorithms (Discrete Structure, BIT152): what the TU syllabus asks and how it has actually been tested, with 14 solved past questions from this unit.

Trees and Graph Algorithms in short

Trees are the simplest connected graphs and are used for file systems, expressions and searching. This chapter also covers important graph algorithms: minimum spanning trees, traversals, Euler and Hamilton paths, and shortest paths.

Trees and Graph Algorithms: notes

Tree definition and properties

A tree is a connected undirected graph with no simple cycles. A forest is a graph whose components are all trees.

Key properties of a tree with $n$ vertices:

  • There is exactly one simple path between any two vertices.
  • It has exactly $n - 1$ edges.
  • Removing any edge disconnects it. Adding any edge creates exactly one cycle.
  • Every tree with at least two vertices has at least two leaves (vertices of degree 1).

A rooted tree has one vertex chosen as the root, and all edges point away from it. Terms:

  • Parent, child, siblings: relations between connected vertices.
  • Leaf: a vertex with no children. Internal vertex: a vertex with children.
  • Level of a vertex: length of the path from the root. Height: the largest level.
  • A binary tree has at most two children per vertex (left and right).
  • A full $m$-ary tree has exactly $m$ children for each internal vertex.

For a full $m$-ary tree with $i$ internal vertices: $n = mi + 1$ vertices and $\ell = (m-1)i + 1$ leaves. For example, a full binary tree with 5 internal vertices has $n = 11$ vertices and $6$ leaves. An $m$-ary tree of height $h$ has at most $m^h$ leaves.

Spanning trees

A spanning tree of a connected graph $G$ is a subgraph that is a tree and contains every vertex of $G$. A graph with $n$ vertices has a spanning tree with $n - 1$ edges.

A spanning tree can be found by removing edges that lie on cycles, one at a time, until no cycle is left. A connected graph has a spanning tree.

Example. The triangle with edges $ab, bc, ca$ has 3 spanning trees: remove any one edge. Each has 2 edges.

Spanning trees are built by depth-first search or breadth-first search. Their use includes network design: connect all computers with the fewest cables.

Minimum spanning trees

In a weighted graph each edge has a number (cost, distance). A minimum spanning tree (MST) is a spanning tree with the smallest possible total weight. It answers: what is the cheapest way to connect all places?

Two classic algorithms:

  • Prim's algorithm: start from any vertex. Repeatedly add the cheapest edge that joins a tree vertex to a non-tree vertex.
  • Kruskal's algorithm: sort edges by weight. Add edges in order, skipping any that would form a cycle.

Both give a tree of the same minimum total weight (the tree itself may differ if edge weights tie). With $n$ vertices, the MST has $n - 1$ edges.

What this unit covers

  • Tree definition and properties
  • Spanning trees
  • Minimum spanning trees
  • Kruskal's algorithm
  • Tree traversal methods
  • Pre-order traversal
  • In-order traversal
  • Post-order traversal
  • Euler paths and circuits
  • Hamilton paths and circuits
  • Cut vertices and cut edges
  • Dijkstra's algorithm for shortest paths

Euler paths and circuits

2082 ยท 5 marks

Mention the necessary and sufficient conditions for Euler path and Euler circuit with example. [5]

Show solved answer

Euler Path and Euler Circuit: Necessary and Sufficient Conditions

Definitions

  • Euler Path: A path in a graph that visits every edge exactly once.
  • Euler Circuit: A closed path (circuit) in a graph that visits every edge exactly once and starts and ends at the same vertex.

Conditions for Euler Path

Necessary and Sufficient Conditions:

A connected graph G has an Euler Path if and only if it has exactly two vertices of odd degree.

The path must start at one odd-degree vertex and end at the other odd-degree vertex.

Example:

Consider the graph:

A --- B --- C
|         |
D ------- E

Edges: A-B, B-C, C-E, E-D, D-A, B-E

Degree of each vertex:

VertexDegree
A2 (even)
B3 (odd)
C2 (even)
D2 (even)
E3 (odd)
  • Exactly two odd-degree vertices: B and E
  • Therefore, an Euler Path exists, starting at B and ending at E (or vice versa).
  • Example path: B โ†’ A โ†’ D โ†’ E โ†’ C โ†’ B โ†’ E

Conditions for Euler Circuit

Necessary and Sufficient Conditions:

A connected graph G has an Euler Circuit if and only if every vertex has even degree.

Since the circuit must return to the starting vertex, every time we enter a vertex we must also be able to leave it, requiring even degree for all vertices.

Example:

Consider the graph:

A --- B
|   / |
|  /  |
| /   |
C --- D

Edges: A-B, A-C, B-C, B-D, C-D

Degree of each vertex:

VertexDegree
A2 (even)
B3 (odd)
C3 (odd)
D2 (even)

This graph does NOT have an Euler Circuit (B and C have odd degree).

Now consider a simpler graph -- a cycle C4:

A --- B
|     |
D --- C

Edges: A-B, B-C, C-D, D-A

VertexDegree
A2 (even)
B2 (even)
C2 (even)
D2 (even)
  • All vertices have even degree
  • Euler Circuit exists: A โ†’ B โ†’ C โ†’ D โ†’ A

Summary Table

ConditionEuler PathEuler Circuit
All vertices even degreeYes (also a circuit)Yes
Exactly 2 odd-degree verticesYesNo
More than 2 odd-degree verticesNoNo
Graph must beConnectedConnected

Key Point: An Euler Circuit is a special case of an Euler Path where the start and end vertex are the same, requiring the stricter condition that all vertices have even degree.

Open this answer on its own page โ†’

2079 ยท 10 marks

State necessary and sufficient conditions for a graph to have Euler path and circuit.Find the GCD of 12 and 18 using Extended Euclidian Algorithm.[4+6]

  • Numbers for GCD computation: $a = 12$, $b = 18$ - Required: necessary and sufficient conditions for Euler path and Euler circuit; GCD via Extended Euclidean Algorithm expressing $\gcd$ as a linear combination. No data missing. --- - Euler Path: A trail that uses every edge of the graph exactly ...
Open this answer on its own page โ†’

2078 ยท 5 marks

What is Euler path? Compare it with Hamilton path. [5]

An Euler path (also called an Eulerian path) is a path in a graph that visits every edge exactly once. If such a path starts and ends at the same vertex, it is called an Euler circuit (or Eulerian circuit). - A connected graph has an Euler path if and only if it has exactly two vertices of odd de...

Open this answer on its own page โ†’

Dijkstra's algorithm for shortest paths

2082 ยท 5 marks

Find the shortest path from a to z in following graph using Dijkstraโ€™s algorithm. [5]

Show solved answer

Given

Problem: Find shortest path from $a$ to $z$ using Dijkstra's algorithm.

Critical issue: The question refers to a graph ("following graph"), but no graph image, edge list, or weight data is provided in the text supplied to me.

Dijkstra's algorithm requires:

  • The set of vertices
  • The set of edges connecting them
  • The weight/cost of each edge

None of these are present in the question. The graph is entirely missing.


Solution

Because the actual graph is unavailable, I cannot compute the genuine shortest path for this specific exam problem. The correct answer depends entirely on the edges and weights shown in the missing figure, which cannot be recovered or guessed.

What I can provide: the method (procedure marks)

Dijkstra's algorithm for source $a$ to destination $z$:

  1. Initialize $dist[a] = 0$, $dist[v] = \infty$ for all other $v$; mark all unvisited; set $prev[v]$ = undefined.
  2. Select the unvisited vertex $u$ with the smallest $dist[u]$.
  3. Relax each unvisited neighbor $v$ of $u$: $$\text{if } dist[u] + w(u,v) < dist[v] \implies dist[v] = dist[u] + w(u,v),; prev[v] = u$$
  4. Mark $u$ as visited.
  5. Repeat steps 2 to 4 until $z$ is visited (or all vertices processed).
  6. Trace back from $z$ via $prev[\cdot]$ to obtain the shortest path.

Note on the Missing Graph

Because the figure is missing, the trace below uses a standard textbook graph with these edges and weights:

$$a\text{-}b=4,; a\text{-}c=7,; b\text{-}c=2,; b\text{-}d=3,; b\text{-}e=2,; c\text{-}d=3,; d\text{-}z=5,; e\text{-}z=1$$

This data does not come from the question. The resulting path:

$$\text{Path } a \to b \to e \to z = 7$$

is internally consistent for that assumed graph (the arithmetic is correct: $4 + 2 + 1 = 7$), but it corresponds to a graph that was not given, so it cannot be validated as the answer to the actual exam problem.

Conclusion

The graph data is missing. No definitive shortest path or distance can be computed. To solve this problem, the vertex set, edge set, and edge weights from the figure are required.

If the intended graph is the one used here, the answer $a \to b \to e \to z$ with cost $7$ follows, but that cannot be confirmed without the original figure.

Open this answer on its own page โ†’

2079 ยท 5 marks

Write the Dijkstra's algorithm to find the shortest path between two nodes in graph. [5]

Show solved answer

Dijkstra's Algorithm for Shortest Path

Definition

Dijkstra's algorithm is a greedy algorithm that finds the shortest path from a source node to all other nodes (or a specific destination node) in a weighted graph with non-negative edge weights.


Algorithm Steps

DIJKSTRA(Graph G, source s):

1. Initialize:
   - dist[s] = 0          (distance to source is 0)
   - dist[v] = โˆž          (distance to all other vertices is infinity)
   - prev[v] = NULL        (previous node in optimal path)
   - Q = set of all vertices (unvisited set / priority queue)

2. While Q is not empty:
   a. u = vertex in Q with minimum dist[u]   (extract-min)
   b. Remove u from Q
   c. For each neighbor v of u (still in Q):
      - alt = dist[u] + weight(u, v)
      - If alt < dist[v]:
          dist[v] = alt        (relaxation step)
          prev[v] = u

3. Return dist[], prev[]

Pseudocode (Formal)

function Dijkstra(G, s):
    for each vertex v in G:
        dist[v] โ† โˆž
        prev[v] โ† UNDEFINED
    dist[s] โ† 0
    Q โ† all vertices of G

    while Q โ‰  โˆ…:
        u โ† vertex in Q with min dist[u]
        remove u from Q

        for each neighbor v of u:
            if v โˆˆ Q:
                alt โ† dist[u] + w(u, v)
                if alt < dist[v]:
                    dist[v] โ† alt
                    prev[v] โ† u

    return dist[], prev[]

Worked Example

Consider the graph:

    A --4-- B
    |       |
    2       1
    |       |
    C --3-- D
     \     /
      5   2
       \ /
        E

Find shortest path from A to E.

StepVisiteddist[A]dist[B]dist[C]dist[D]dist[E]
Init-0โˆžโˆžโˆžโˆž
1A042โˆžโˆž
2C04257
3B04257
4D04257
5E04257

Shortest path: A โ†’ C โ†’ D โ†’ E with cost = 7


Key Properties

PropertyDetail
Time ComplexityO(Vยฒ) with array; O((V+E) log V) with min-heap
Space ComplexityO(V)
ConditionEdge weights must be non-negative
TypeGreedy algorithm

Important Notes

  • The relaxation step (if alt < dist[v]) is the core operation
  • Dijkstra's algorithm fails with negative weight edges (use Bellman-Ford instead)
  • It produces a shortest path tree rooted at the source node
Open this answer on its own page โ†’

2080.1 ยท 5 marks

Write Dijkstraโ€™s algorithm to find the shortest path from source node to goal node. [5]

Dijkstra's algorithm finds the shortest path from a source node to all other nodes (or a specific goal node) in a weighted graph with non-negative edge weights. --- - dist[] : array storing shortest distance from source to each node - visited[] : boolean array marking finalized nodes - prev[] : a...

Open this answer on its own page โ†’

0 ยท 10 marks

What is shortest path finding problem? Use Dijkstra's algorithm to find the length of the shortest path between a and z in the given weighted graphs.[10]

Show solved answer

Shortest Path Finding Problem and Dijkstra's Algorithm

Given

  • Algorithm required: Dijkstra's algorithm
  • Source vertex: $a$
  • Destination vertex: $z$
  • Graph: "the given weighted graph" - the actual graph image/edge weights are NOT provided in the question text.

Missing data: The specific weighted graph (vertices, edges, and edge weights) is not reproduced in the question. Without it, an exact numeric shortest-path length cannot be computed uniquely.

Because the graph is missing, I will (a) explain the problem and algorithm fully, and (b) demonstrate the method on a representative standard graph, clearly flagged as an assumption. The numeric answer below is valid only for that assumed graph.


Solution

What is the Shortest Path Finding Problem?

The shortest path problem is the task of finding a path between two vertices in a weighted graph $G = (V, E)$ such that the sum of the edge weights along the path is minimum.

  • Weights represent cost, distance, or time.
  • Graph may be directed or undirected, but Dijkstra requires non-negative weights.
  • Variants: single-pair, single-source (one source to all), all-pairs.
  • Applications: GPS routing, network routing protocols (OSPF), telecommunication, robotics.

Dijkstra's Algorithm (greedy)

  1. $d[a] = 0$, $d[v] = \infty$ for all other $v$; all vertices unvisited.
  2. Pick the unvisited vertex $u$ with minimum $d[u]$.
  3. Relax each unvisited neighbour $w$: if $d[u] + w(u,w) < d[w]$, set $d[w] = d[u] + w(u,w)$.
  4. Mark $u$ visited.
  5. Repeat until $z$ is finalized.

Worked Example (assumed standard graph: graph not given in question)

Edges: $a\text{-}b=2,\ a\text{-}d=4,\ a\text{-}e=7,\ b\text{-}c=3,\ b\text{-}e=3,\ c\text{-}f=5,\ c\text{-}z=3,\ d\text{-}e=3,\ e\text{-}f=2,\ f\text{-}z=4$

IterCurrentabcdefz
Init-0โˆžโˆžโˆžโˆžโˆžโˆž
1a (0)0โœ“2โˆž47โˆžโˆž
2b (2)0โœ“2โœ“545โˆžโˆž
3d (4)2โœ“54โœ“5โˆžโˆž
4c (5)5โœ“4โœ“5108
5e (5)5โœ“5โœ“78
6f (7)7โœ“min(8, 7+4=11)=8
7z (8)8โœ“

Relaxation details:

  • From $c$: $z = 5+3 = 8$, $f = 5+5 = 10$.
  • From $e$: $f = 5+2 = 7 < 10 \Rightarrow f = 7$.
  • From $f$: $z = 7+4 = 11 > 8 \Rightarrow$ no update.

Result (for the assumed graph)

$$d[z] = 8$$

Shortest path: $a \to b \to c \to z$ with length $2 + 3 + 3 = 8$.

Note: This numeric answer depends entirely on the assumed edge set. For the actual exam graph, apply the same procedure to the given weights.

Open this answer on its own page โ†’

Tree definition and properties

2082 ยท 5 marks

Define spanning and minimum spanning tree? How do you traverse tree? [2+3]

A spanning tree of a connected, undirected graph G = (V, E) is a subgraph that: - Includes all the vertices of the graph - Is a tree (connected and acyclic) - Has exactly V - 1 edges A graph with n vertices will have a spanning tree with exactly n - 1 edges. Example: For a graph with 4 vertices (...

Open this answer on its own page โ†’

Cut vertices and cut edges

2081 ยท 5 marks

Define cut vertices and cut edges. How do you determine whether the graph has Euler path? [5]

A cut vertex (or articulation point) of a connected graph G is a vertex whose removal (along with all edges incident to it) increases the number of connected components of the graph. In other words, vertex v is a cut vertex if G is connected but G - v is disconnected. Example: Here, vertex C is a...

Open this answer on its own page โ†’

Tree traversal methods

2080 ยท 5 marks

Describe pre-order, postorder and inorder traversal of a tree with an example. [5]

Tree traversal means visiting every node of a tree exactly once in a systematic way. The three standard depth-first traversal methods differ in when the root (parent) node is visited relative to its subtrees. --- --- Algorithm: 1. Visit the root node 2. Recursively traverse the left subtree 3. Re...

Open this answer on its own page โ†’

Pre-order traversal

2080 ยท 5 marks

Define tree traversal. Explain pre-order traversal with example. [5]

Tree traversal is the process of visiting each node in a tree data structure exactly once in a systematic and well-defined order. It is used to process, search, or display all the nodes of a tree. Unlike linear data structures (arrays, linked lists), trees can be traversed in multiple ways. --- I...

Open this answer on its own page โ†’

Minimum spanning trees

2078 ยท 10 marks

Define spanning tree and minimum spanning tree with suitable example. Use Kruskal's algorithms to find minimum spanning tree in the given graph[10]

The question refers to "the given graph," but no graph (vertices, edges and weights) is reproduced with the question text available here. The working below therefore uses the standard 9-vertex textbook MST example so that the method is demonstrated in full. It is not guaranteed to be the graph pr...

Open this answer on its own page โ†’

Kruskal's algorithm

2080.1 ยท 5 marks

How does Kruskal algorithm work? Illustrate with an example. [5]

Kruskal's algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph. It builds the MST by always picking the smallest available edge that does not form a cycle. --- 1. Sort all edges in non-decreasing order of their weights. 2. Initial...

Open this answer on its own page โ†’

Spanning trees

0 ยท 5 marks

Define spanning tree. Explain minimum spanning tree with example. [5]

A spanning tree of a connected, undirected graph G = (V, E) is a subgraph that: - Includes all vertices of the graph - Is a tree (connected and acyclic) - Has exactly V - 1 edges A graph with n vertices will have a spanning tree with exactly n - 1 edges. --- A Minimum Spanning Tree is a spanning ...

Open this answer on its own page โ†’