0% found this document useful (0 votes)
20 views

Graph Traversal Tech

graph traversal

Uploaded by

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

Graph Traversal Tech

graph traversal

Uploaded by

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

Lecture 5

Graphs
[Part 1]

1
Lecture Content

1. Graph Basics
1.1 Definitions and Terminologies
1.2 Data Structures Used to Store Graphs
2. Graph Traversal
2.1 Depth-First Search (DFS)
2.2. Breadth-First Search (BFS)

2
Lecture Content

3. Topological Sorting

Course prerequisites
Topologically sorted order: C F B A E D G H
3
1. Graph Basics

• Tree generalizes linear structures (i.e., singly


linked list), graph generalizes tree.

4
1. Graph Basics

• The key difference between tree and graph is that,


in a graph, there may be more than one path
between two nodes.
• Many real-world problems can be modeled by
using graphs. For example,
- finding the fastest routes for a mass
transportation system (answers to the question:
what is the shortest driving route from city A to city
B),

5
1. Graph Basics

- finding a minimum spanning tree (answers to


the question: how can computers be connected with
the least amount of cable)
- routing electronic email through a computer
network.

6
1. Graph Basics

7
1.1 Definitions and Terminologies

• A graph G consists of a set of vertices (also called


nodes) V and a set of edges (also called arcs) E that
connect the vertices.
• That is, G = (V, E), where V is a set of n vertices
{v0, v1, …, vn-1} and E is a set of m edges {e0, e1,
…, em-1}.
• Each edge e  E is a pair (u, v), where u, v  V
(i.e., e = (u, v)).

8
1.1 Definitions and Terminologies

• The number of vertices and edges of a graph G is


denoted as |V| and |E|, respectively (i.e., |V| = n and
|E| = m).

• If each edge e = (u, v) in G is ordered (i.e., (u, v) ≠


(v, u)), the graph is called directed graph (also
called digraph). Otherwise, the graph is called
undirected graph.

9
1.1 Definitions and Terminologies

• If a graph is directed, the in-degree of a vertex is


the number of edges entering it.
• The out-degree of a vertex is the number of
edges leaving it.
• The degree of a vertex is the sum of its in-degree
and out-degree.

10
1.1 Definitions and Terminologies

• If each edge e = (u, v) in G has a weight w (also


called cost or length), the graph is called weighted
graph.

• Vertex v is adjacent (also called neighbor) to


vertex u if there is an edge from u to v.

• A path in a graph is a sequence of vertices


connected by edges.
11
1.1 Definitions and Terminologies

• In unweighted graphs, a path length is the


number of edges on the path.

• The distance between two vertices is the length of


the shortest path between them.

• A weighted path length is the sum of weights


(costs or lengths) on the path.

12
1.1 Definitions and Terminologies

• If |E| = (|V|2), then the graph G is called dense


graph.

• If |E| = (|V|), then the graph G is called sparse


graph.

• A cycle is a path from a vertex back to itself.

13
1.1 Definitions and Terminologies

• A graph with no cycle is called acyclic graph. A


directed acyclic graph is called a DAG.
• A graph in which every pair of vertices is
connected by a path is said to be connected.
• Let G be a simple graph (i.e., no parallel edges
and no self-loop/cycle) with n vertices and m edges.
If G is undirected, then m ≤ n(n - 1)/2. If G is
directed, then m ≤ n(n - 1).

14
1.2 Data Structures Used to Store Graphs

• A graph can be stored by using an adjacency


matrix (i.e., two-dimensional array, also called
neighbor matrix) or an adjacency list (i.e., singly
linked list).
• Normally, dense and sparse graphs are represented
by using adjacency matrix and an adjacency list,
respectively.

15
Adjacency Matrix

• An adjacency matrix (e.g., int adjMat[][])


is a two-dimensional array in which the elements
indicate whether an edge is present between two
vertices. If a graph has n vertices, an n×n
adjacency matrix is needed to store the graph.

16
Adjacency Matrix

• Example: Consider the following graph

17
Adjacency Matrix

• The adjacency matrix for the graph is as follows.

18
Adjacency List

• An adjacency list is an array of singly linked lists.


Each individual list shows what vertices a given
vertex is adjacent to.
• The adjacency lists for the graph is given next.

19
Lecture Content

1. Graph Basics
1.1 Definitions and Terminologies
1.2 Data Structures Used to Store Graphs
2. Graph Traversal
2.1 Depth-First Search (DFS)
2.2. Breadth-First Search (BFS)

20
2. Graph Traversal

• Three traversals of a tree are preorder, inorder,


and postorder. Tree traversal is always starts at the
root of the tree.
• Two traversals of a graph are depth-first search
(DFS) and breadth-first search (BFS). Since a graph
has no root, we must specify a vertex at which to
begin a traversal.
• Depth-first search is essentially a generalization of
the preorder traversal of a rooted tree.

21
2.1 Depth-First Search (DFS)

• Example: List the order in which the nodes of the


undirected graph shown in the figure below are
visited by a depth-first traversal that starts from
vertex a. Assume that we choose to visit adjacent
vertices in alphabetical order.

22
2.1 Depth-First Search (DFS)

Algorithm DFS // M.H. Alsuwaiyel


Input: A directed or undirected graph G = (V, E).
Output: Numbering of the vertices in
depth-first search order.
1. predfn ← 1; postdfn ← 1
2. for each vertex v  V
3. mark v unvisited
4. end for
5. for each vertex v  V
6. if v is marked unvisited then dfs(v) // starting vertex
7. end for 23
2.1 Depth-First Search (DFS)

Procedure dfs(v) // v is starting vertex, using stack


1. S ← {v} // insert v into stack
2. mark v visited
3. while S ≠ {}
4. v ← Peek(S) // v is current vertex
5. find an unvisited neighbor w of v

24
2.1 Depth-First Search (DFS)

6. if w exists then
7. Push(w, S)
8. mark w visited
9. predfn ← predfn + 1
10. else
11. Pop(S); postdfn ← postdfn + 1
12. end if
13. end while

25
2.1 Depth-First Search (DFS)

• The stack contents during DFS are given below.

26
2.1 Depth-First Search (DFS)

• The order in which the nodes are visited by a DFS


that starts from vertex a is a, b, c, d, e, f, g, h, i, j.
• The resulting tree (i.e., the depth-first search tree)
is

27
2.1 Depth-First Search (DFS)

• Tree edges: edges in the depth-first search tree.


An edge (v, w) is a tree edge if w was first visited
when exploring the edge (v, w).
• Back edges: All other edges.

28
2.1 Depth-First Search (DFS)

In depth-first search traversal of directed graphs,


however, the edges of G are classified into four
types:
• Tree edges: edges in the depth-first search tree.
An edge (v, w) is a tree edge if w was first visited
when exploring the edge (v, w).
• Back edges: edges of the form (v, w) such that w
is an ancestor of v in the depth-first search tree
(constructed so far) and vertex w was marked
visited when (v, w) was explored.
29
2.1 Depth-First Search (DFS)

• Forward edges: edges of the form (v, w) such that


w is a descendant of v in the depth-first search tree
(constructed so far) and vertex w was marked
visited when (v, w) was explored.
• Cross edges: All other edges.

30
2.1 Depth-First Search (DFS)

Procedure dfs(v) // v is starting vertex, using recursion


1. mark v visited
2. predfn ← predfn + 1
3. for each edge (v, w)  E
4. if w is marked unvisited then dfs(w)
5. end for
6. postdfn ← postdfn + 1

31
2.2 Breadth-First Search (BFS)

• Depth-first search algorithm gets as far away from


the starting point as quickly as possible. DFS is
implemented using a stack.
• In contrast, breadth-first search algorithm stays as
close as possible to the starting point. BFS visits all
the vertices adjacent to the starting vertex, and then
goes further afield. BFS is implemented using a
queue.
• The level-order traversal of a tree is an example of
the breadth-first traversal.
32
2.2 Breadth-First Search (BFS)

• Example: List the order in which the nodes of the


undirected graph shown in the figure below are
visited by a breadth-first traversal that starts from
vertex a. Assume that we choose to visit adjacent
vertices in alphabetical order.

33
2.2 Breadth-First Search (BFS)

Algorithm BFS // M.H. Alsuwaiyel


Input: A directed or undirected graph G = (V, E).
Output: Numbering of the vertices in BFS order.
1. bfn ← 1
2. for each vertex v  V
3. mark v unvisited
4. end for
5. for each vertex v  V
6. if v is marked unvisited then bfs(v) // starting vertex
7. end for
34
2.2 Breadth-First Search (BFS)

Procedure bfs(v) // v is starting vertex, using queue


1. Q ← {v} // insert v into queue
2. mark v visited
3. while Q ≠ {}
4. v ← dequeue(Q) // v is current vertex
5. for each edge (v, w)  E
6. if w is marked unvisited then
7. enqueue(w, Q)
8. mark w visited
9. bfn ← bfn + 1
10. end if 35
2.2 Breadth-First Search (BFS)

11. end for


12. end while

36
2.2 Breadth-First Search (BFS)

• The queue contents during BFS are given below.

37
2.2 Breadth-First Search (BFS)

• The order in which the nodes are visited by a BFS


that starts from vertex a is a, b, g, c, f, h, d, e, i, j.
• The resulting tree (i.e., the breadth-first search
tree) is

38
2.2 Breadth-First Search (BFS)

• Tree edges: edges in the breadth-first search tree.


An edge (v, w) is a tree edge if w was first visited
when exploring the edge (v, w).
• Cross edges: All other edges.

39
3. Topological Sorting

• Given a directed acyclic graph (dag for short) G =


(V, E), the problem of topological sorting is to find
a linear ordering of its vertices in such a way that if
(v, w)  E, then v appears before w in the ordering.

40
3. Topological Sorting

• For example, one possible topological sorting of


the vertices in the dag shown in figure (a) above is
b, d, a, c, f, e, g (or a, b, d, c, e, f, g)
• We will assume that the dag has only one vertex,
say s, of indegree 0. If not, we may simply add a
new vertex s and edges from s to all vertices of
indegree 0.

41
3. Topological Sorting

• Next, we simply carry out a depth-first search on


G starting at vertex s.
• When the traversal is complete, the values of the
counter postdfn define a reverse topological
ordering of the vertices in the dag.
• Thus, to obtain the ordering, we may add an
output step to Algorithm DFS just after the counter
postdfn is incremented. The resulting output is
reversed to obtain the desired topological ordering.

42
Exercises

1. Write a complete program to implement the DFS.


2. Write a complete program to implement the BFS.
3. Modify the DFS program to find the
topologically sorted order of a given dag.

43
Exercises

4. List the order in which the nodes of the


undirected graph shown in the figure below are
visited by a depth-first traversal that starts from
vertex a. Repeat this exercise for a depth-first
traversal starting from vertex d.

44
Exercises

5. List the order in which the nodes of the


undirected graph shown in the figure below are
visited by a breadth-first traversal that starts from
vertex a. Repeat this exercise for a breadth-first
traversal starting from vertex d.

45
References

1. Noel Kalicharan. 2008. Data Structures in Java.


CreateSpace. ISBN: 143827517X. Chapter 5
2. Noel Kalicharan. 2008. Data Structures in C.
Createspace Press. ISBN: 1438253273.
Chapter 7
3. Robert Lafore. 2002. Data Structures and
Algorithms in Java. 2nd Ed, SAMS.
ISBN: 0672324539.

46
References

4. M.H. Alsuwaiyel. 1999. Algorithms Design


Techniques and Analysis. World Scientific
Publishing. ISBN: 9810237405. Chapters 1, 4, 6
5. Anany V. Levitin. 2011. Introduction to the
Design and Analysis of Algorithms. 3Ed.
Addison-Wesley. ISBN: 0132316811.

47

You might also like