Unit 3: Graphs and Trees
2024-07-12
Primitive types, arrays, records,
string and string processing,
data representation in memory,
pointers and references, linked structures,
knowledge of hashing function,
use of stacks, queues, graphs and trees,
and strategies for choosing the right data structure.
Node A container of data and connections
Edges A connection between nodes
Branches a multiple edges from a node (2 or more)
Chain
Tree
Graph
Course Description: This course provides a comprehensive introduction to fundamental data structures and their implementations. Students will gain a solid understanding of how data is organized and manipulated within computer systems, and how to select appropriate data structures for different problem-solving scenarios.
Teaching Methods:
Assessment:
Row No. | Binary tree | Teritary tree | 4th tree |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 3 | 4 | 5 |
3 | 7 | 13 | 21 |
4 | 15 | 40 | 85 |
Isomeric Graph A
n1 | n2 | n3 | n4 | n5 | |
---|---|---|---|---|---|
n1 | e1 | e6 | |||
n2 | e1 | e2 | e5 | ||
n3 | e2 | e3 | |||
n4 | e6 | e3 | e4 | ||
n5 | e5 | e4 |
Isomeric Graph B
n1 | n2 | n3 | n4 | n5 | |
---|---|---|---|---|---|
n1 | e1 | e6 | |||
n2 | e1 | e2 | e5 | ||
n3 | e2 | e3 | |||
n4 | e6 | e3 | e4 | ||
n5 | e5 | e4 |
Graphic rendering
Weighted, Directed Graph
From / To | A | B | C | D | E | F | G |
---|---|---|---|---|---|---|---|
A | 3 | ||||||
B | 1 | ||||||
C | 3 | 3 | |||||
D | 5 | ||||||
E | 2 | 2 | 2 | ||||
F | 2 | ||||||
G | 2 |
Unweighted, Directed Graph
From / To | A | B | C | D | E | F | G |
---|---|---|---|---|---|---|---|
A | 1 | ||||||
B | 1 | ||||||
C | 1 | 1 | |||||
D | 1 | ||||||
E | 1 | 1 | 1 | ||||
F | 1 | ||||||
G | 1 |
Undirected, Weighted
From / To | A | B | C | D | E | F | G |
---|---|---|---|---|---|---|---|
A | 1 | 3 | |||||
B | 1 | 5 | |||||
C | 3 | 3 | 3 | ||||
D | 5 | 3 | 2 | ||||
E | 3 | 2 | 2 | 2 | |||
F | 2 | ||||||
G | 2 |
Undirected, Unweighted
From / To | A | B | C | D | E | F | G |
---|---|---|---|---|---|---|---|
A | 1 | 1 | |||||
B | 1 | 1 | |||||
C | 1 | 1 | 1 | ||||
D | 1 | 1 | 1 | ||||
E | 1 | 1 | 1 | 1 | |||
F | 1 | ||||||
G | 1 |
One Step
A B C D E F G
A 0 0 1 0 0 0 0
B 1 0 0 0 0 0 0
C 0 0 0 1 1 0 0
D 0 1 0 0 0 0 0
E 0 0 0 1 0 1 1
F 0 0 0 0 1 0 0
G 0 0 0 0 1 0 0
Two Steps
\[\tiny T=\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\]
A B C D E F G
A 0 0 0 1 1 0 0
B 0 0 1 0 0 0 0
C 0 1 0 1 0 1 1
D 1 0 0 0 0 0 0
E 0 1 0 0 2 0 0
F 0 0 0 1 0 1 1
G 0 0 0 1 0 1 1
Three steps
\[\tiny T=\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\]
A B C D E F G
A 0 1 0 1 0 1 1
B 0 0 0 1 1 0 0
C 1 1 0 0 2 0 0
D 0 0 1 0 0 0 0
E 1 0 0 2 0 2 2
F 0 1 0 0 2 0 0
G 0 1 0 0 2 0 0
Four steps
A B C D E F G
A 1 1 0 0 2 0 0
B 0 1 0 1 0 1 1
C 1 0 1 2 0 2 2
D 0 0 0 1 1 0 0
E 0 2 1 0 4 0 0
F 1 0 0 2 0 2 2
G 1 0 0 2 0 2 2
Five steps
A B C D E F G
A 1 0 1 2 0 2 2
B 1 1 0 0 2 0 0
C 0 2 1 1 5 0 0
D 0 1 0 1 0 1 1
E 2 0 0 5 1 4 4
F 0 2 1 0 4 0 0
G 0 2 1 0 4 0 0
Path A to G (Undirected,Unweighted)
A | B | C | D | E | F | G |
---|---|---|---|---|---|---|
* | (A) | (A) | 0 | 0 | 0 | 0 |
0 | (1) | (1) | ||||
* | A | A | B | 0 | 0 | |
0 | 1 | (1) | (2) | |||
* | A | A | B | C | 0 | 0 |
0 | 1 | 1 | (2) | (2) | ||
* | A | A | B | C | E | E |
0 | 1 | 1 | 2 | 2 | (3) | (3) |
* | A | A | B | C | E | E |
0 | 1 | 1 | 2 | 2 | 3 | (3) |
* | A | A | B | C | E | E |
0 | 1 | 1 | 2 | 2 | 3 | 3 |
G -> E -> C -> A; Distance: 3 units
Path A to G (Directed,Weighted)
A | B | C | D | E | F | G |
---|---|---|---|---|---|---|
* | (A) | 0 | 0 | 0 | 0 | |
0 | (3) | |||||
* | A | (C) | (C) | 0 | 0 | |
0 | 3 | (6) | (6) | |||
* | (D) | A | C | (C) | 0 | 0 |
0 | (11) | 3 | 6 | (6) | ||
* | (D) | A | C | C | (E) | (E) |
0 | (11) | 3 | 6 | 6 | (8) | (8) |
* | (D) | A | C | C | E | (E) |
0 | (11) | 3 | 6 | 6 | 8 | (8) |
* | (D) | A | C | C | E | E |
0 | (11) | 3 | 6 | 6 | 8 | 8 |
* | D | A | C | C | E | E |
0 | 11 | 3 | 6 | 6 | 8 | 8 |
G -> E -> C -> A; 8 units
Singapore -> Malaysia -> Burma ->
Bhutan -> India -> Pakistan ->
Afghanistan -> Tajikista -> Kyrgyzstan ->
Uzbekistan -> Kazakhstan -> Russia ->
Lithuania -> Poland -> Germany -> Netherlands ->
United Kingdom
Start: Raffles Hotel, Singapore
End: SeaPoint, Cape Town, South Africa
Distance: 20,392 km
Time:4,581 hr
Given this graph:
Hamiltonian circuit: visits every vertex once with no repeats. Being a circuit, it must start and end at the same vertex. (All vertices have even number of edges.)
Hamiltonian path: visits every vertex once with no repeats, but does not start and end at the same vertex. (At most 2 vertices with odd number of edges.)
(Only works when you start and end on the vertices with odd number of edges. Only two or less vertices with odd numbered edges are permitted)
Works from any starting point and always ends up at the starting point.
More than 2 vertices with odd number of edges
IT221 Discrete Math