You are on page 1of 183

# Data Structure and Algorithm Analysis

Slide Set 6
Trees & Graph
13 CS- I & II
Engr. Maria Shaikh
maria.sheikh@faculty.muet.edu.pk

## Non-Linear Data Structure

These data structures do not have their elements in a sequence. Trees and
Graph is an example.
Trees are mainly used to represent data containing a hierarchical relationship

10/30/2014

## Non Linear Data Structures

Non Linear Data Structures are
Branched recursive data structures
Consisting of nodes
Each node can be connected to other nodes

## Examples of tree-like structures

Trees: binary / other degree, balanced, etc.
Graphs: directed / undirected, weighted, etc.
Networks

Network
Tree

1
Project
Manager

3
Team

Developer
1

Designer

Developer
3

QA Team

Tester 1

Tester 2

1
Developer
2

4
21

Graph
4

14

11

12

19

31

## Basic Terminologies in Trees

Nodes: The boxes on the tree are called nodes.
Children: The nodes immediately below (to the left and right of) a given node
are called its children.
Parent: The node immediately above a given node is called its parent.
Root: The (unique) node without a parent is called the root.
Leaf: A node with no children is called a leaf
Siblings: Two children of the same parent are said to be siblings.
A node can be an ancestor (e.g.grandparent) or a descendant (e.g. greatgrandchild).
10/30/2014

## Basic Terminologies in Trees

Root: node with no parent.
A non-empty tree has exactly one root.
leaf: node with no children
siblings: two nodes with the same parent.

10/30/2014

## Basic Terminologies in Graph

Path: a sequence of nodes n1 , n2, ---------- nk such that ni is the
parent of ni+1
for 1 i < k,
In other words, a sequence of hops to get from one node to
Another.
The length of a path is the number of edges in the path, or 1 less
than the number of nodes in it.

10/30/2014

## Engr. Maria Shaikh

Trees
Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node
Interior nodes
Leaf nodes

Height

## Trees Data Structures

Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent

Binary tree
Tree with 02 children per node
Binary Tree
Tree

a

b
d
g

c
e

10

f
j

## The size of a binary tree is the number of nodes

in it
This tree has size 12
The depth of a node is its distance from the root.
a is at depth zero
e is at depth 2
The depth of a binary tree is the depth of its
deepest node
This tree has depth 4

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

## Engr. Maria Shaikh

12

Binary Trees
Each node has at most 2 children

Right
Subtree

Root
node

Left
subtree

17
9
6

15
5

10

Left child
13

Right child

## Complete Binary Tree

A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are as
far left as possible.

10/30/2014

14

15

left son

right son

leaves

## Binary Search Trees

Binary search trees are ordered
For each node x in the tree
All the elements of the left subtree of x are < x
All the elements of the right subtree of x are x

17

## Binary Search Trees

A Binary Search Tree is a binary tree with the following properties:
All items in the left subtree are less than the root.
All items in the right subtree are greater or equal to the root.
Each subtree is itself a binary search tree.
Binary search trees provide an excellent structure for searching a list
and at the same time for inserting and deleting data into the list.

## Balanced Binary Tree

a

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.

## (a), (b) - complete and balanced trees;

(d) nearly complete and balanced tree;

## (c), (e) neither complete nor balanced trees

Engr. Maria Shaikh

## Binary Search Tree Operations

We discuss four basic BST operations: traversal, search, insert, and delete; and
develop algorithms for searches, insertion, and deletion.

Traversals
Searches
Insertion
Deletion

## TRAVERSING BINARY TREES

One of the common operations of a binary tree is to traverse the tree. Traversing
a tree is to pass through all of its nodes once. You may want to print the
contents of each node or to process the contents of the nodes. In either case each
node of the tree is visited.
There are three main traversal methods where traversing a binary tree involves
visiting the root and traversing its left and right subtrees. The only difference
among these three methods is the order in which these three operations are
performed.

## TRAVERSING BINARY TREES

Traversing a binary tree in preorder (depth-first order)
1. Visit the root.
2. Traverse the left subtree in preorder.
3. Traverse the right subtree in preorder.

## Traversing a binary tree in preorder

Preorder: ABDGCEHIF

Preorder Traversal

23 18 12 20 44 35 52

## Engr. Maria Shaikh

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 Traversing Binary Trees

Traversing a binary tree in inorder (or symmetric order)
1. Traverse the left subtree in inorder.
2. Visit the root.
3. Traverse the right subtree in inorder.

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

## Binary Expression Trees

[a + (b c)] * [(d e) / (f + g h)]

+
a

/
-

c
Preorder: * + a b c / - d e - + f g h

Postorder: a b c - + d e f g + h - / *
10/30/2014

37

Traversing

## a tree means to visit each of its nodes exactly one in

particular order
Many traversal algorithms are known
Depth-First Search (DFS)
Visit node's successors first
Usually implemented by recursion

Nearest nodes visited first

Implemented by a queue
38

## Depth-First Search (DFS)

Depth-First Search first

## visits all descendants of given node

recursively, finally visits the node itself

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

Stack: 7
Output: (empty)

19

12

21

31

14

23

40

Stack: 7, 19
Output: (empty)

19

12

21

31

14

23

41

Stack: 7, 19, 1
Output: (empty)

19

12

21

31

14

23

42

Stack: 7, 19
Output: 1

19

12

21

31

14

23

43

Stack: 7, 19, 12
Output: 1

19

12

21

31

14

23

44

Stack: 7, 19
Output: 1, 12

19

12

21

31

14

23

45

Stack: 7, 19, 31
Output: 1, 12

19

12

21

31

14

23

46

## DFS in Action (Step 8)

Stack: 7, 19
Output: 1, 12, 31

19

12

21

31

14

23

47

## DFS in Action (Step 9)

Stack: 7
Output: 1, 12, 31, 19

19

12

21

31

14

23

48

## DFS in Action (Step 10)

Stack: 7, 21
Output: 1, 12, 31, 19

19

12

21

31

14

23

49

## DFS in Action (Step 11)

Stack: 7
Output: 1, 12, 31, 19, 21

19

12

21

31

14

23

50

## DFS in Action (Step 12)

Stack: 7, 14
Output: 1, 12, 31, 19, 21

19

12

21

31

14

23

51

## DFS in Action (Step 13)

Stack: 7, 14, 23
Output: 1, 12, 31, 19, 21

19

12

21

31

14

23

52

## DFS in Action (Step 14)

Stack: 7, 14
Output: 1, 12, 31, 19, 21, 23

19

12

21

31

14

23

53

## DFS in Action (Step 15)

Stack: 7, 14, 6
Output: 1, 12, 31, 19, 21, 23

19

12

21

31

14

23

54

## DFS in Action (Step 16)

Stack: 7, 14
Output: 1, 12, 31, 19, 21, 23, 6

19

12

21

31

14

23

55

## DFS in Action (Step 17)

Stack: 7
Output: 1, 12, 31, 19, 21, 23, 6, 14

19

12

21

31

14

23

56

## DFS in Action (Step 18)

Stack: (empty)
Output: 1, 12, 31, 19, 21, 23, 6, 14, 7

Traversal finished
7

19

12

21

31

14

23

57

## visits the neighbor nodes, later their

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

Queue: 7
Output: 7

19

12

21

31

14

23

59

Queue: 7, 19
Output: 7

19

12

21

31

14

23

60

Queue: 7, 19, 21
Output: 7

19

12

21

31

14

23

61

## BFS in Action (Step 4)

Queue: 7, 19, 21, 14
Output: 7

19

12

21

31

14

23

62

## BFS in Action (Step 5)

Queue: 7, 19, 21, 14
Output: 7, 19

19

12

21

31

14

23

63

## BFS in Action (Step 6)

Queue: 7, 19, 21, 14, 1
Output: 7, 19

19

12

21

31

14

23

64

## BFS in Action (Step 7)

Queue: 7, 19, 21, 14, 1, 12
Output: 7, 19

19

12

21

31

14

23

65

## BFS in Action (Step 8)

Queue: 7, 19, 21, 14, 1, 12, 31
Output: 7, 19

19

12

21

31

14

23

66

## BFS in Action (Step 9)

Queue: 7, 19, 21, 14, 1, 12, 31
Output: 7, 19, 21

19

12

21

31

14

23

67

## BFS in Action (Step 10)

Queue: 7, 19, 21, 14, 1, 12, 31
Output: 7, 19, 21, 14

19

12

21

31

14

23

68

## BFS in Action (Step 11)

Queue: 7, 19, 21, 14, 1, 12, 31, 23
Output: 7, 19, 21, 14

19

12

21

31

14

23

69

## BFS in Action (Step 12)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14

19

12

21

31

14

23

70

## BFS in Action (Step 13)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14, 1

19

12

21

31

14

23

71

## BFS in Action (Step 14)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14, 1, 12

19

12

21

31

14

23

72

## BFS in Action (Step 15)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14, 1, 12, 31

19

12

21

31

14

23

73

## BFS in Action (Step 16)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14, 1, 12, 31, 23

19

12

21

31

14

23

74

## BFS in Action (Step 16)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

19

12

21

31

14

23

75

## BFS in Action (Step 17)

Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6

The
queue is
empty
stop

## Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

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

## General Tree T with 13 Nodes

A, B, C, D, E, F, G, H, J, K, L, M, N

L
10/30/2014

## Engr. Maria Shaikh

78

Presentation topic

GRAPH

## Engr. Maria Shaikh

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

## Other Definitions of Graph

A data structure that consists of a set of nodes (vertices) and a set of
edges that relate the nodes to each other.
The set of edges describes relationships among the vertices.
Graph is a mathematical structure used to model pair wise
relations between objects from a certain collection.

10/30/2014

81

## Formal definition of graphs

A graph G is defined as follows:
G = (V,E) is composed of:
V(G): a finite, nonempty set of vertices
E(G): a set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:
a
b V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),
(d,e) }

e
Engr. Maria Shaikh

Types of graphs
There are two types of graph :
Directed graph
Undirected graph

## Engr. Maria Shaikh

Directed graph
A graphs G is called directed graph or digraph if each edge has a direction.

## Engr. Maria Shaikh

Un Directed graph
A graphs G is called directed graph if each edge has no direction.

## Engr. Maria Shaikh

Graph terminologies
by an edge

## Path: A sequence of vertices that connect two nodes in a graph

Complete graph: A graph in which every vertex is directly
connected to every other vertex.

## Graph terminologies (cont.)

What is the number of edges in a complete directed graph with N vertices?
N * (N-1)

O( N )

## Graph terminologies (cont.)

What is the number of edges in a complete undirected graph with N vertices?
N * (N-1) / 2

O( N )

## Engr. Maria Shaikh

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.

## Engr. Maria Shaikh

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)

## Some different types of graph

1. Sub graph

2. Complete graph

## Some different types of graph

3. Weighted graph:
A graph in which each edge carries a value.

## Engr. Maria Shaikh

Representation of graphs
Graphs can be represented in 2 ways :
Array representation

## Real Life Example

CS16

electronic circuits

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

## Engr. Maria Shaikh

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

## Engr. Maria Shaikh

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

## Engr. Maria Shaikh

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

If (v0, v1) is an edge in an undirected graph,
The edge (v0, v1) is incident on vertices v0 and v1

## If <v0, v1> is an edge in a directed graph

The edge <v0, v1> is incident on v0 and v1

## Engr. Maria Shaikh

More
Tree - connected graph without cycles.
Forest - collection of trees.

tree

## A forest is an undirected graph

without cycles
The connected components of a
forest are trees.

tree

forest
tree
tree

Forest
Engr. Maria Shaikh

## Cycles and trees

Graph with no cycles: acyclic
Directed Acyclic Graph: DAG
Undirected forest:
Acyclic undirected graph

## Tree: undirected acyclic connected graph

one connected component

## 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

## Engr. Maria Shaikh

More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1

n5
m4

If m < n - 1, G is
not connected

n5
m3

## Engr. Maria Shaikh

Graph Representations

## Engr. Maria Shaikh

Representing graphs
When graph is dense
|E| close to |V|2

When graph is sparse
|E| << |V|2

## Engr. Maria Shaikh

113

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
matrix for a digraph need not be symmetric

## Engr. Maria Shaikh

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

## |V| |V| matrix A.

Number vertices from 1 to |V| in some arbitrary manner.
A is then given by:
A[i, j ]

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

## Engr. Maria Shaikh

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

From the adjacency matrix, to determine the connection of vertices is
easy
n 1
The degree of a vertex is
j 0
For a digraph (= directed graph), the row sum is the out_degree, while
the column sum is the in_degree
n 1

n 1

j 0

j 0

## Engr. Maria Shaikh

(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

## Only for connected graphs

Example:
Web spider or crawler is the data collecting part of a search engine that
visits all the hypertext documents on the web. (The documents are
vertices and the hyperlinks are the edges).
Engr. Maria Shaikh

120

## Graph Search (traversal)

How do we search a graph?
At a particular vertices, where shall we go next?
Two common framework:
the depth-first search (DFS)
In DFS, go as far as possible along a single path until reach a dead

## end (a vertex with no edge out or no neighbor unexplored) then

backtrack
In BFS, one explore a graph level by level away (explore all
neighbors first and then move on)
Engr. Maria Shaikh1.121

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

## BFS in a binary tree

BFS: visit all siblings before their descendants
5

2
1

8
3

10

5 2 8 1 3 6 10 7 9
Engr. Maria Shaikh

9
123

## BFS for General Graphs

This version assumes vertices have two children
left, right
This is trivial to fix

## But still no good for general graphs

It does not handle cycles

124

## Example for Undirected Graph

A

Queue: A
B

E
C

Engr. Maria Shaikh

125

Queue: A B E

A
B

E
C

## B and E are next

Engr. Maria Shaikh

126

## Example for Undirected Graph (cont.)

Queue: A B E C G D F

E
C

## When we go to B, we put G and C in the queue

When we go to E, we put D and F in the queue
Engr. Maria Shaikh

127

## Example for Undirected Graph (cont.)

Queue: A B E C G D F

E
C

## When we go to B, we put G and C in the queue

When we go to E, we put D and F in the queue
Engr. Maria Shaikh

128

## Example for Undirected Graph (cont.)

Queue: A B E C G D F F

E
C

## Suppose we now want to expand C. We put F in the queue again!

Engr. Maria Shaikh

129

Generalizing BFS
Cycles:
We need to save auxiliary information
Each node needs to be marked
Visited:
Not visited:

## No need to be put on queue

Put on queue when found

## What about assuming only two children vertices?

Need to put all adjacent vertices in queue

130

## The general BFS algorithm

Each vertex can be in one of three states:
Unmarked and not on queue
Marked and on queue
Marked and off queue

## Engr. Maria Shaikh

131

Handling vertices
Unmarked and not on queue:
Not reached yet

## Marked and on queue:

Known, but adjacent vertices not visited yet (possibly)

## Marked and off queue:

Known, all adjacent vertices on queue or done with

132

## Example for BFS Undirected Graph Modified

Queue: A

A
B

E
C

Engr. Maria Shaikh

133

## Example for BFS Undirected Graph Modified

Queue: A B E

A
B

E
C

Mark them and put them in queue.
Engr. Maria Shaikh

134

Queue: A B E C G

E
C

## Now take B off queue, and queue its neighbors.

Engr. Maria Shaikh

135

## Example for BFS Undirected Graph Modified

A

Queue: A B E C G D F
B

E
C

Do same with E.
Engr. Maria Shaikh

136

## Example for BFS Undirected Graph Modified

Queue: A B E C G D F

E
C

Visit C.
Its neighbor F is already marked, so not queued.
Engr. Maria Shaikh

137

## Example for BFS Undirected Graph Modified

Queue: A B E C G D F

E
C

Visit G.
Engr. Maria Shaikh

138

## Example for BFS Undirected Graph Modified

Queue: A B E C G D F

E
C

## Visit D. F, E marked so not queued.

Engr. Maria Shaikh

139

## Example for Undirected Graph Modified

Queue: A B E C G D F

E
C

Visit F.
E, D, C marked, so not queued again.
Engr. Maria Shaikh

140

## Example for BFS Undirected Graph Modified

Queue: A B E C G D F

E
C

## Done. We have explored the graph in order:

A B E C G D F.
Engr. Maria Shaikh

141

The Color Scheme

## White vertices have not been discovered

All vertices start out white

## Grey vertices are discovered but not fully explored

They may be adjacent to white vertices

## Black vertices are discovered and fully explored

They are adjacent only to black and gray vertices

vertices

An Example
A

1.143

1.144

## Data Structure and Algorithm

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

## Interesting features of BFS

Complexity: O(|V| + |E|)
All vertices put on queue exactly once
For each vertex on queue, we expand its edges
In other words, we traverse all edges once

## BFS finds shortest path from s to each vertex

Shortest in terms of number of edges
Why does this work?

152

## Depth-First Search (DFS)

The basic idea behind this algorithm is that it traverses the graph using

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

## Example for DFS Undirected Graph Modified

Current vertex: A

A
B

E
C

Engr. Maria Shaikh

154

Current: B

A
B

E
C

## Mark it and re-visit.

Engr. Maria Shaikh

155

Current: C

A
B

E
C

## Now expand B, and visit its neighbor, C.

Engr. Maria Shaikh

156

## Example for DFS Undirected Graph Modified

Current: F

A
B

E
C

Visit F.
Pick one of its neighbors, E.
Engr. Maria Shaikh

157

A

Current: E
B

E
C

## Es adjacent vertices are A, D and F.

A and F are marked, so pick D.
Engr. Maria Shaikh

158

Current: D

A
B

E
C

## Visit D. No new vertices available. Backtrack to E. Backtrack to F.

Backtrack to C. Backtrack to B
Engr. Maria Shaikh

159

Current: G

A
B

E
C

## Visit G. No new vertices from here. Backtrack to B. Backtrack to A. E

Engr. Maria Shaikh

160

1

Current:

A
5

E
6

## Done. We have explored the graph in order:

ABCFEDG
Engr. Maria Shaikh

161

## DFS: Color Scheme

Vertices initially colored white

## Then colored gray when discovered

Then black when finished

## DFS: Time Stamps

Discover time d[u]: when u is first discovered
Finish time f[u]: when backtrack from u

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

## Interesting features of DFS

Complexity: O(|V| + |E|)
All vertices visited once, then marked
For each vertex on queue, we examine all edges
In other words, we traverse all edges once

Why?

## Engr. Maria Shaikh

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