SlideShare a Scribd company logo
1
Lecture 5
Graphs
[Part 1]
2
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)
3
Lecture Content
3. Topological Sorting
Course prerequisites
Topologically sorted order: C F B A E D G H
4
1. Graph Basics
• Tree generalizes linear structures (i.e., singly
linked list), graph generalizes tree.
5
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),
6
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.
7
1. Graph Basics
.
8
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)).
9
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.
10
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.
11
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.
12
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.
13
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.
14
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).
15
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.
16
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.
17
Adjacency Matrix
• Example: Consider the following graph
18
Adjacency Matrix
• The adjacency matrix for the graph is as follows.
19
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.
20
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)
21
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.
22
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.
23
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
24
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
25
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
26
2.1 Depth-First Search (DFS)
• The stack contents during DFS are given below.
27
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
28
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.
29
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.
30
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.
31
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
32
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.
33
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.
34
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
35
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
36
2.2 Breadth-First Search (BFS)
11. end for
12. end while
37
2.2 Breadth-First Search (BFS)
• The queue contents during BFS are given below.
38
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
39
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.
40
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.
41
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.
42
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.
43
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.
44
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.
45
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.
46
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.
47
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.

More Related Content

Similar to U1 L5 DAA.pdf

22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
Kumar
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar
 

Similar to U1 L5 DAA.pdf (20)

Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphs
GraphsGraphs
Graphs
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graphs
GraphsGraphs
Graphs
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 

Recently uploaded

一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
MAQIB18
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Domenico Conte
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
StarCompliance.io
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 

Recently uploaded (20)

Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPsWebinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflictSupply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
 

U1 L5 DAA.pdf

  • 2. 2 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)
  • 3. 3 Lecture Content 3. Topological Sorting Course prerequisites Topologically sorted order: C F B A E D G H
  • 4. 4 1. Graph Basics • Tree generalizes linear structures (i.e., singly linked list), graph generalizes tree.
  • 5. 5 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),
  • 6. 6 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.
  • 8. 8 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)).
  • 9. 9 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.
  • 10. 10 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.
  • 11. 11 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.
  • 12. 12 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.
  • 13. 13 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.
  • 14. 14 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).
  • 15. 15 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.
  • 16. 16 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.
  • 17. 17 Adjacency Matrix • Example: Consider the following graph
  • 18. 18 Adjacency Matrix • The adjacency matrix for the graph is as follows.
  • 19. 19 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.
  • 20. 20 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)
  • 21. 21 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.
  • 22. 22 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.
  • 23. 23 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
  • 24. 24 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
  • 25. 25 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
  • 26. 26 2.1 Depth-First Search (DFS) • The stack contents during DFS are given below.
  • 27. 27 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
  • 28. 28 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.
  • 29. 29 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.
  • 30. 30 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.
  • 31. 31 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
  • 32. 32 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.
  • 33. 33 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.
  • 34. 34 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
  • 35. 35 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
  • 36. 36 2.2 Breadth-First Search (BFS) 11. end for 12. end while
  • 37. 37 2.2 Breadth-First Search (BFS) • The queue contents during BFS are given below.
  • 38. 38 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
  • 39. 39 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.
  • 40. 40 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.
  • 41. 41 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.
  • 42. 42 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.
  • 43. 43 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.
  • 44. 44 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.
  • 45. 45 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.
  • 46. 46 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.
  • 47. 47 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.