0% found this document useful (0 votes)
26 views44 pages

Lecture 05

graph theory slide 5

Uploaded by

Hamayon Wazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views44 pages

Lecture 05

graph theory slide 5

Uploaded by

Hamayon Wazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

11/25/2024

Graph Theory
Lecture - 5

Dr. Ilyas Fakhir


Trees

11/25/2024
•A graph that is connected and has no cycle is called a
tree.
• Generallya graph that does not contain any cycle is
called acyclic graph. Using this notation, we can say
that a connected acyclic graph is a tree.
• These
graphs are connected and have no cycles.
Hence, each of these graphs is a tree.

2
Rooted Trees

11/25/2024
• A rooted tree is a tree where one of the nodes is
designated as the root node. (Only one root in a
tree)
• A rooted tree has a hierarchical structure: the root
on top, followed by the nodes adjacent to it right
below, followed by the nodes adjacent to those1
next, and so2 on. 8 9
3 2 7
10
1 7
5 8 9
5 12
4 6 10
3
11
4 6
11 12

Unrooted tree Tree rooted with root 1 3


Tree-Related Concepts

11/25/2024
• The nodes adjacent to x and below x are
called the children of x, 1

and x is called their parents 3 2 7

•A node that has no


5 8 9
children is called a leaf
• The descendants of a node 10
4 6
are: itself, its children,
11 12
their children, all the way down
• The ancestors of a node are: itself, its
parent, its grandparent, all the way to the
root 4
Tree-Related Concepts (Contd.)

11/25/2024
• Thedepth of a node is the number of edges
from the root to that node. 1

• The depth (or height) of


3 2 7
a rooted tree is the depth
of the lowest leaf 5 8 9

• Depth of node 10: 3


4 6 10
• Depth of this tree: 4

11 12

5
11/25/2024
root internal vertex

descendants of g
ancestor of d

leaf
parent of d

child of c
subtree
sibling of d

6
Forest

11/25/2024
•Aforest is an acyclic graph. The connected
components of a forest are therefore trees,
which explains the use (very natural!) of
the terminology. Forests generalize trees.
Tree Traversal Techniques

11/25/2024
• Threerecursive techniques for binary tree
traversal
• In
each technique, the left subtree is traversed
recursively, the right subtree is traversed
recursively, and the root is visited
• Whatdistinguishes the techniques from one
another is the order of those 3 tasks

8
Preoder, Inorder, Postorder

11/25/2024
• In Preorder, the root Preorder Traversal:
is visited before (pre) 1. Visit the root
2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
• In Inorder, the root is
Inorder Traversal:
visited in-between left 1. Traverse left subtree
and right subtree traversal 2. Visit the root
3. Traverse right subtree
• In Postorder, the root
is visited after (pre) Postorder Traversal:
1. Traverse left subtree
the subtrees traversals 2. Traverse right subtree
3. Visit the root 9
Illustrations for Traversals

11/25/2024
1
• Assume: visiting a node
is printing its label 3 7

• Preorder:
1 3 5 4 6 7 8 9 10 11 12 5 8 9

• Inorder: 10
4 6
4 5 6 3 1 8 7 9 11 10 12
• Postorder: 11 12
4 6 5 3 8 11 12 10 9 7 1

10
11/25/2024
Level Order Traversal
• This is a different traversal than what we
have covered above. Level order traversal
follows BFS(Breadth-First Search) to
visit/modify every node of the tree.
• As BFS suggests, the breadth of the tree
takes priority first and then move to depth. In
simple words, we will visit all the nodes
present at the same level one-by-one from
left to right and then move to the next level
to visit all the nodes of that level.
Example

11/25/2024
Spanning Trees

11/25/2024
• A spanning tree of a graph is just a subgraph
that contains all the vertices and is a tree.
• A graph may have many spanning trees.
• In certain applications it is useful to know the
exact number. We shall write τ(G) for the
number of spanning trees of a graph G.
• One can sometimes calculate τ(G) quite quickly.
If G is a tree, then τ(G) = 1. If G is a cycle of
length n, then n spanning trees can be
constructed, each by deleting one edge, so τ(G)
= n.
13
Example

11/25/2024
•Aspanning tree in G1 must contain one of the edges
xy and one of the edges yz. The number of choices is
2 x 3 = 6. So τ(G1) = 6. Since loops do not affect the
function r , τ(G2) = 4. To calculate τ(G3) , one first
observes that three pairs of vertices must be joined.
This can be done in four ways. In each case there are
eight trees. So τ(G3 ) = 4 x 8 = 32. 14
Complete Graph All 16 of its Spanning Trees, τ(G) = n(n-2) , n number of veritces

11/25/2024
15
Minimal Spanning Trees

11/25/2024
• TheMinimal Spanning Tree for a given graph is
the Spanning Tree of minimum cost for that
graph.
Complete Graph Minimal Spanning Tree

2 2

5 3 3

1 1

16
Algorithms for Obtaining the

11/25/2024
Minimum Spanning Tree

• Kruskal's Algorithm

• Prim's Algorithm

• Boruvka's Algorithm

17
Kruskal's Algorithm

11/25/2024
• This algorithm creates a forest of trees.
Initially the forest consists of n single node
trees (and no edges). At each step, we add
one edge (the cheapest one) so that it joins
two trees together. If it were to form a cycle,
it would simply link two nodes that were
already part of a single connected tree, so
that this edge would not be needed.

18
Kruskal's Algorithm Pseudocode

11/25/2024
1. T (the final spanning tree) is defined to be
the empty set;
2. For each vertex v of G, make the empty set
out of v;
3. Sort the edges of G in ascending (non-
decreasing) order;
4. For each edge (u, v) from the sored list of
step 3.
If u and v belong to different sets
Add (u,v) to T;
Get together u and v in one single set;
5. Return T

19
11/25/2024
• The steps are:

 The forest is constructed - with each node in a


separate tree.
 The edges are placed in a priority queue.
 Until we've added n-1 edges,
 Extract the cheapest edge from the queue,
 If it forms a cycle, reject it,
 Else add it to the forest. Adding it to the forest
will join two trees together.

• Every step will have joined two trees in the


forest together, so that at the end, there will
only be one tree in T.
20
11/25/2024
Complete Graph

B 4 C
4
2 1

A 4 E
1 F

D 2 3
10
G 5

5 6 3

4
I
H

2 3
J

21
11/25/2024
A 4 B A 1 D

B 4 C B 4 D

B 4 C B 10 J C 2 E
4
2 1
C 1 F D 5 H
A 4 E
1 F

2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3

4
I G 3 I G 4 J
H

2 3
J H 2 J I 3 J

22
Sort Edges

11/25/2024
A 1 D C 1 F
(in reality they are placed in a priority queue - not
sorted - but sorting them makes the algorithm
C 2 E E 2 G
easier to visualize)

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

23
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

24
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

25
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

26
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

27
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

28
11/25/2024
Cycle A 1 D C 1 F

Don’t Add Edge


C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

29
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

30
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

31
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

32
11/25/2024
Cycle A 1 D C 1 F

Don’t Add Edge


C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

33
11/25/2024
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

34
11/25/2024
Minimum Spanning Tree Complete Graph

B 4 C 4
B C
4 4
2 1 2 1
A E A 4
1 F E F
1

D 2 2
D 3
10
G G 5

3 5 6 3

4
I I
H H
2 3 3
J 2 J

35
11/25/2024
Assignment

Write Pseudocode the following algorithms and

verify through examples.

• Prim's Algorithm

• Boruvka's Algorithm

36
11/25/2024
Other Kinds of Binary Trees (Full Binary
Trees)
• Full
Binary Tree: A full binary tree is a binary
tree where all the leaves are on the same level
and every non-leaf has two children
• The first four full binary trees are:

37
Examples of Non-Full Binary Trees

11/25/2024
• These
trees are NOT full binary trees: (do you
know why?)

38
Canonical Labeling of Full Binary Trees

11/25/2024
• Label
the nodes from 1 to n from the top to the
bottom, left to right

1
1 1 2 3
2 3
4 5 6 7
1
2 3 Relationships between labels
5 6 7 of children and parent:
4 i
8 9 10 111213 14 15 2i 2i+1

39
11/25/2024
Other Kinds of Binary Trees
(Almost
40 Complete Binary trees)

• Almost Complete Binary Tree: An almost complete


binary tree of n nodes, for any arbitrary nonnegative
integer n, is the binary tree made up of the first n
nodes of a canonically labeled full binary
1
1 1
1 1
2 3
2 2 3
2 1
1 4 5 6
4 2 3
2 3
4 5
4 5 6 7
Depth/Height of Full Trees and Almost

11/25/2024
Complete Trees

• The height (or depth ) h of such trees is O(log n)


• Proof:In the case of full trees,
 The number of nodes n is: n = 1+2+22+23+…+2h ---- (i)
 Multiply by 2 2n = 2+22+23+…+2h+1 ---- (ii)
 Subtract (i) from (ii) n = 2h+1– 1
 Therefore, 2h+1 = n+1, and thus by taking log base 2,
log(2h+1 )= log(n+1) (h+1)log2=log(n+1)
 h+1 = log(n+1) h = log(n+1) – 1
 Hence, h=O(log n)
• For
almost complete trees, the proof is left as an
exercise. 41
11/25/2024
Canonical Labeling of
Almost Complete Binary Trees
• Same labeling inherited from full binary trees
• Same relationship holding between the labels of
children and parents:

Relationships between labels


of children and parent:
i
2i 2i+1

42
Array Representation of Full Trees

11/25/2024
and Almost Complete Trees
• A canonically label-able tree, like full binary
trees and almost complete binary trees, can
be represented by an array A of the same
length as the number of nodes
• A[k] is identified with node of label k
• That is, A[k] holds the data of node k
• Advantage:
 no need to store left and right pointers in the
nodes  save memory
 Direct access to nodes: to get to node k,
access A[k]
43
Example: In any graph or multigraph, the sum of

11/25/2024
the degrees of the vertices equals twice the number
of edges.

• If G = (V, E) be a non-directed graph with vertices


V = {V1, V2,…Vn}. The sum of the entries in
column j is 2, since each edge is incident with two
vertices; the sum over all columns is thus 2e, so
that

• If G = (V, E) be a directed graph with vertices V =


{V1, V2,…Vn}, then

44

You might also like