SlideShare a Scribd company logo
1 of 47
Download to read offline
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 Graph Lecture on Basics, Traversal, and Topological Sorting

Similar to Graph Lecture on Basics, Traversal, and Topological Sorting (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

Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Data Warehouse , Data Cube Computation
Data Warehouse   , Data Cube ComputationData Warehouse   , Data Cube Computation
Data Warehouse , Data Cube Computationsit20ad004
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/managementakshesh doshi
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 

Recently uploaded (20)

Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Data Warehouse , Data Cube Computation
Data Warehouse   , Data Cube ComputationData Warehouse   , Data Cube Computation
Data Warehouse , Data Cube Computation
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/management
 
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 

Graph Lecture on Basics, Traversal, and Topological Sorting

  • 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.