Trees and Graphs
Trees and Graphs
Trees and Graphs
Slide Set 6
Trees & Graph
13 CS- I & II
Engr. Maria Shaikh
maria.sheikh@faculty.muet.edu.pk
10/30/2014
Network
Tree
1
Project
Manager
3
Team
Leader
Developer
1
Designer
Developer
3
QA Team
Leader
Tester 1
Tester 2
1
Developer
2
4
21
Graph
4
14
11
12
19
31
10/30/2014
10/30/2014
Trees
Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node
Interior nodes
Leaf nodes
Height
Binary tree
Tree with 02 children per node
Binary Tree
Tree
b
d
g
c
e
10
f
j
Trees
Data structure terminology
Node, edge, root, child, children, siblings, parent, ancestor,
descendant, predecessor, successor, internal node, leaf, depth,
height, subtree
17
Height = 2
6
11
Depth 0
15
14
5
Depth 1
Depth 2
Binary Trees
A binary tree is a finite set of elements that are either empty or is
partitioned into three disjoint subsets. The first subset contains a
single element called the root of the tree. The other two subsets are
themselves binary trees called the left and right subtrees of the
original tree. A left or right subtree can be empty.
Each element of a binary tree is called a node of the tree.
10/30/2014
12
Binary Trees
Binary trees: most widespread form
Each node has at most 2 children
Right
Subtree
Root
node
Left
subtree
17
9
6
15
5
10
Left child
13
Right child
10/30/2014
14
15
right son
leaves
17
a
b
d
c
e
c
g
h i
j
A balanced binary tree
f
g
i j
An unbalanced binary tree
A binary tree is balanced if every level above the lowest is full (contains 2n nodes)
In most applications, a reasonably balanced binary tree is desirable.
Traversals
Searches
Insertion
Deletion
Preorder: ABDGCEHIF
Preorder Traversal
23 18 12 20 44 35 52
Postorder Traversal
Traversing a binary tree in postorder
1. Traverse the left subtree in postorder.
2. Traverse the right subtree in postorder.
3. Visit the root.
Postorder Traversal
12 20 18 35 52 44 23
Engr. Maria Shaikh
Postorder Traversal
Postorder: GDBHIEFCA
Engr. Maria Shaikh
Inorder Traversal
12 18 20 23 35 44 52
Inorder traversal of a binary search tree produces a sequenced list
Engr. Maria Shaikh
Inorder Traversal
Inorder: DGBAHEICF
Right-Node-Left Traversal
52 44 35 23 20 18 12
Right-node-left traversal of a binary search tree produces a descending sequence
Engr. Maria Shaikh
+
a
/
-
c
Preorder: * + a b c / - d e - + f g h
Postorder: a b c - + d e f g + h - / *
10/30/2014
37
Implemented by a queue
38
DFS algorithm
pseudo code
9
7
DFS(node)
{
for each child c of node
DFS(c);
print the current node;
}
19
2
21
3
12
31
14
7
23
6
39
19
12
21
31
14
23
40
19
12
21
31
14
23
41
19
12
21
31
14
23
42
19
12
21
31
14
23
43
19
12
21
31
14
23
44
19
12
21
31
14
23
45
19
12
21
31
14
23
46
19
12
21
31
14
23
47
19
12
21
31
14
23
48
19
12
21
31
14
23
49
19
12
21
31
14
23
50
19
12
21
31
14
23
51
19
12
21
31
14
23
52
19
12
21
31
14
23
53
19
12
21
31
14
23
54
19
12
21
31
14
23
55
19
12
21
31
14
23
56
Traversal finished
7
19
12
21
31
14
23
57
neighbors, etc.
BFS algorithm pseudo code
BFS(node)
{
queue node
while queue not empty
v queue
print v
for each child c of v
queue c
}
7
3
19
6
21
7
12
31
14
9
23
6
58
19
12
21
31
14
23
59
19
12
21
31
14
23
60
19
12
21
31
14
23
61
19
12
21
31
14
23
62
19
12
21
31
14
23
63
19
12
21
31
14
23
64
19
12
21
31
14
23
65
19
12
21
31
14
23
66
19
12
21
31
14
23
67
19
12
21
31
14
23
68
19
12
21
31
14
23
69
19
12
21
31
14
23
70
19
12
21
31
14
23
71
19
12
21
31
14
23
72
19
12
21
31
14
23
73
19
12
21
31
14
23
74
19
12
21
31
14
23
75
The
queue is
empty
stop
19
12
21
31
14
23
76
General Trees
A general tree ( sometimes called a tree) is defined to be a non empty finite set T of
elements, called nodes, such that:
1) T contains a distinguished element R, called the root of T.
2) The remaining elements of T form an ordered collection of zero or more disjoint trees
T1, T2, -------------TM are called successors of R.
General Trees
A
B
E
D
C
K
G
L
10/30/2014
78
Presentation topic
GRAPH
What is graph ?
A GRAPH is a mathematical structure consisting of a set of vertices and a set
of edges.
Node: Each element of a graph is called node of a graph.
Edge :Line joining two nodes is called an edge.
= Vertices
= Edeges
Engr. Maria Shaikh
10/30/2014
81
e
Engr. Maria Shaikh
Types of graphs
There are two types of graph :
Directed graph
Undirected graph
Directed graph
A graphs G is called directed graph or digraph if each edge has a direction.
Un Directed graph
A graphs G is called directed graph if each edge has no direction.
Graph terminologies
Adjacent nodes: Two nodes are adjacent if they are connected
by an edge
5 is adjacent to 7
7 is adjacent from 55
O( N )
O( N )
Terminologies
connected graph: any two vertices are connected by some path
connected
not connected
subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g., the graph below has 3 connected
components.
Subgraphs Examples
0
1
2
3
G1
0
1
(i)
2
3
0
1
(ii)
(iii)
(a) Some of the subgraph of G1
G3
3 (iv)
2
(i)
(ii)
(iii)
(b) Some of the subgraph of G3
(iv)
2. Complete graph
Representation of graphs
Graphs can be represented in 2 ways :
Array representation
electronic circuits
networks (roads, flights, communications)
JFK
LAX
HNL
Engr. Maria Shaikh
STL
DFW
FTL
Paths
A path in a graph is a sequence of vertices such that from
each of its vertices there is an edge to the next vertex in the
sequence.
The length of a path is the number of edges on it. The length can be zero
for the case of a single vertex.
Engr. Maria Shaikh
Paths
A path may be infinite.
A finite path always has a first vertex, called its start vertex, and a last vertex,
called its end vertex.
Both of them are called terminal vertices of the path.
The other vertices in the path are internal vertices.
A
Path= A B D C E
E
A- Start vertex
E- End vertex
Path
3
3
3
c
e d
d
abedc
e
bedc
101
Simple Paths
A graph with no loops or multiple edges is called a simple graph.
A path with no repeated vertices is called a simple path.
The path from v1 to v4 is said to be simple path as
vertices is touched more than once.
The path from v1 to v4 is not simple as
v1 is touched twice or looped.
cycle:
Simple path, except that the last vertex is the same as
the first vertex. Its also known as a circuit or circular path.
A
B
E
Path= A E C A
Paths
a
simple path
b
bec
c
e
cycle:
b
acda
c
d
Engr. Maria Shaikh
Degree
The degree of vertex in an undirected graph is the number of edges incident to
that vertex.
A vertex with degree one is called pendent vertex or end vertex.
A vertex with degree zero and hence has no incident edges is called an isolated
vertex.
A
V1
B
Pendent vertex
Isolated vertex
In the undirected graph vertex v3 has the degree 3 And vertex v2 has the degree 2
Degree
Degree in directed graph
Degree of directed graph has two types
i. Indegree
No of edges with their head towards the vertex.
ii. Outdegree
No of edges with their tail towards the vertex.
Indegree of vertex v2 is 2 and
Outdegree of vertex v1 is 1.
Engr. Maria Shaikh
Example:
3
0
2
3
3
2
3G
3
1
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
Directed graph
in-degree
out-degree
G3
Engr. Maria Shaikh
G2
More
Tree - connected graph without cycles.
Forest - collection of trees.
tree
tree
forest
tree
tree
Forest
Engr. Maria Shaikh
109
Connectivity
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice!
Therefore, intuitively, m = n(n -1)/2.
n 5
m (5
More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1
n5
m4
If m < n - 1, G is
not connected
n5
m3
Graph Representations
Adjacency Matrix
Adjacency Lists
Representing graphs
Adjacency matrix:
When graph is dense
|E| close to |V|2
Adjacency lists:
When graph is sparse
|E| << |V|2
113
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is symmetric; the adjacency
matrix for a digraph need not be symmetric
Examples
1
1
2
3
1
0
0
1
2
0
1
1
3
1
0
0
1
2
3
4
Engr. Maria Shaikh
1
0
1
1
0
2
1
0
0
0
3
1
0
0
0
4
0
0
0
0
115
Adjacency Matrix
d4
1
2
3
4
1
0
0
0
0
2
1
0
0
0
3
1
1
0
0
4
1
0
1
0
1
2
3
4
1
0
1
1
1
2
1
0
1
0
3
1
1
0
1
4
1
0
1
0
1 if (i, j ) E
aij
0 otherwise
1
0
1
1 1
0 1
1
1 1
3
1
1
1
1
2
3
0 1 0
1
0
1
0 0 0
G2
G1
n2/2
undirected:
directed: n2
symmetric
7
0
1
0
0
0
0
1 1
0 0
0 0
1 1
0 0
0 0
0 0
0 0
0 0 0 0 0
1 0 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 0 1 0 1
0 0 0 1 0
G4
n 1
j 0
j 0
Adjacency Lists
(data structures)
3
0
1
2
3
1
0
0
0
2
2
1
1
G1
0
1
2
1
0
2
G3
3
3
3
2
0
1
0
1
2
3
4
5
6
7
1
0
0
1
5
4
5
6
G4
2
3
3
2
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Traversing a Graph
A traversal of a graph is a systematic procedure for exploring a
graph by examining all of its vertices and edges.
Until finding a goal vertex or until no more vertices
120
Breadth-first search
Level-by-level traversal.
Search for all vertices that are directly reachable from the root (called
level 1 vertices)
After mark all these vertices, visit all vertices that are directly reachable
from any level 1 vertices (called level 2 vertices), and so on.
In general, level k vertices are directly reachable from a level k 1
vertices
122
2
1
8
3
10
5 2 8 1 3 6 10 7 9
Engr. Maria Shaikh
9
123
124
Queue: A
B
E
C
125
A
B
E
C
126
E
C
127
E
C
128
E
C
129
Generalizing BFS
Cycles:
We need to save auxiliary information
Each node needs to be marked
Visited:
Not visited:
130
131
Handling vertices
Unmarked and not on queue:
Not reached yet
132
A
B
E
C
133
A
B
E
C
134
E
C
135
Queue: A B E C G D F
B
E
C
Do same with E.
Engr. Maria Shaikh
136
E
C
Visit C.
Its neighbor F is already marked, so not queued.
Engr. Maria Shaikh
137
E
C
Visit G.
Engr. Maria Shaikh
138
E
C
139
E
C
Visit F.
E, D, C marked, so not queued again.
Engr. Maria Shaikh
140
E
C
141
vertices
An Example
A
1.143
1.144
1.145
C
2
M
Data Structure and Algorithm
1.146
C
2
3
2
3
1.147
3
2
3
1.148
3
2
3
1.149
3
2
3
1.150
3
2
3
1.151
152
recursion
Go as far as possible until you reach a dead end
Backtrack to the previous path and try the next branch
The graph below, started at node a, would be visited in the following
order: a, b, c, g, h, i, e, d, f, j
a
c
e
j
1.153
A
B
E
C
154
A
B
E
C
155
A
B
E
C
156
A
B
E
C
Visit F.
Pick one of its neighbors, E.
Engr. Maria Shaikh
157
Current: E
B
E
C
158
A
B
E
C
159
A
B
E
C
160
Current:
A
5
E
6
161
DFS Example
source
vertex
Engr. Maria1.164
Shaikh
DFS Example
source
vertex
1 |
DFS Example
source
vertex
1 |
2 |
DFS Example
source
vertex
1 |
2 |
3 |
DFS Example
source
vertex
1 |
2 |
3 | 4
DFS Example
source
vertex
1 |
2 |
3 | 4
5 |
DFS Example
source
vertex
1 |
2 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
13|
5 | 6
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
13|
5 | 6
14|
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
13|
5 | 6
14|15
DFS Example
source
vertex
1 |12
8 |11
2 | 7
9 |10
3 | 4
13|16
5 | 6
14|15
181
Graph Traversal
Problem: Search for a certain node or traverse all nodes in the
graph
Depth First Search
Once a possible path is found, continue the search until the end of the
path
10/30/2014
183