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.
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 answerHide 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:
| Vertex | Degree |
|---|---|
| A | 2 (even) |
| B | 3 (odd) |
| C | 2 (even) |
| D | 2 (even) |
| E | 3 (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:
| Vertex | Degree |
|---|---|
| A | 2 (even) |
| B | 3 (odd) |
| C | 3 (odd) |
| D | 2 (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
| Vertex | Degree |
|---|---|
| A | 2 (even) |
| B | 2 (even) |
| C | 2 (even) |
| D | 2 (even) |
- All vertices have even degree
- Euler Circuit exists: A โ B โ C โ D โ A
Summary Table
| Condition | Euler Path | Euler Circuit |
|---|---|---|
| All vertices even degree | Yes (also a circuit) | Yes |
| Exactly 2 odd-degree vertices | Yes | No |
| More than 2 odd-degree vertices | No | No |
| Graph must be | Connected | Connected |
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.
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]
2078 ยท 5 marks
What is Euler path? Compare it with Hamilton path. [5]
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 answerHide 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$:
- Initialize $dist[a] = 0$, $dist[v] = \infty$ for all other $v$; mark all unvisited; set $prev[v]$ = undefined.
- Select the unvisited vertex $u$ with the smallest $dist[u]$.
- 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$$
- Mark $u$ as visited.
- Repeat steps 2 to 4 until $z$ is visited (or all vertices processed).
- 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.
2079 ยท 5 marks
Write the Dijkstra's algorithm to find the shortest path between two nodes in graph. [5]
Show solved answerHide 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.
| Step | Visited | dist[A] | dist[B] | dist[C] | dist[D] | dist[E] |
|---|---|---|---|---|---|---|
| Init | - | 0 | โ | โ | โ | โ |
| 1 | A | 0 | 4 | 2 | โ | โ |
| 2 | C | 0 | 4 | 2 | 5 | 7 |
| 3 | B | 0 | 4 | 2 | 5 | 7 |
| 4 | D | 0 | 4 | 2 | 5 | 7 |
| 5 | E | 0 | 4 | 2 | 5 | 7 |
Shortest path: A โ C โ D โ E with cost = 7
Key Properties
| Property | Detail |
|---|---|
| Time Complexity | O(Vยฒ) with array; O((V+E) log V) with min-heap |
| Space Complexity | O(V) |
| Condition | Edge weights must be non-negative |
| Type | Greedy 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
2080.1 ยท 5 marks
Write Dijkstraโs algorithm to find the shortest path from source node to goal node. [5]
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 answerHide 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)
- $d[a] = 0$, $d[v] = \infty$ for all other $v$; all vertices unvisited.
- Pick the unvisited vertex $u$ with minimum $d[u]$.
- Relax each unvisited neighbour $w$: if $d[u] + w(u,w) < d[w]$, set $d[w] = d[u] + w(u,w)$.
- Mark $u$ visited.
- 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$
| Iter | Current | a | b | c | d | e | f | z |
|---|---|---|---|---|---|---|---|---|
| Init | - | 0 | โ | โ | โ | โ | โ | โ |
| 1 | a (0) | 0โ | 2 | โ | 4 | 7 | โ | โ |
| 2 | b (2) | 0โ | 2โ | 5 | 4 | 5 | โ | โ |
| 3 | d (4) | 2โ | 5 | 4โ | 5 | โ | โ | |
| 4 | c (5) | 5โ | 4โ | 5 | 10 | 8 | ||
| 5 | e (5) | 5โ | 5โ | 7 | 8 | |||
| 6 | f (7) | 7โ | min(8, 7+4=11)=8 | |||||
| 7 | z (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.
Tree definition and properties
2082 ยท 5 marks
Define spanning and minimum spanning tree? How do you traverse tree? [2+3]
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]
Tree traversal methods
2080 ยท 5 marks
Describe pre-order, postorder and inorder traversal of a tree with an example. [5]
Pre-order traversal
2080 ยท 5 marks
Define tree traversal. Explain pre-order traversal with example. [5]
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]
Kruskal's algorithm
2080.1 ยท 5 marks
How does Kruskal algorithm work? Illustrate with an example. [5]
Spanning trees
0 ยท 5 marks
Define spanning tree. Explain minimum spanning tree with example. [5]
Make Unit 9 stick
Practice BIT152 with flashcards & quizzes