SlideShare a Scribd company logo
1 of 100
In science one tries to tell people, in such a way
as to be understood by everyone, something
that no one ever knew before.
But in poetry, it's the exact opposite.
Paul Dirac
Graph
vertex
edge
Weighted Graph
5
3
-2
5
1
0
Undirected Graph
Complete Graph (Clique)
Path
a
c
d e
b
Length = 4
Cycle
a
c
g
d e
b
f
Directed Acyclic Graph (DAG)
Degree
In-Degree Out-Degree
Disconnected Graph
Connected Components
Formally
A weighted graph G = (V, E, w), where
 V is the set of vertices
 E is the set of edges
 w is the weight function
Variations of (Simple) Graph
Multigraph
More than one edge between two
vertices
E is a bag, not a set
Hypergraph
Edges consists of more than two vertex
Example
V = { a, b, c }
E = { (a,b), (c,b), (a,c) }
w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) }
a
bc
4
1
-3
Adjacent Vertices
adj(v) = set of vertices adjacent to v
adj(a) = {b, c}
adj(b) = {}
adj(c) = {b}
∑v |adj(v)| = |E|
adj(v): Neighbours of v
a
bc
4
1
-3
city
direct
flight
5
cost
QuestionWhat is the shortest way to travel between A and
B?
“SHORTEST PATH PROBLEM”
How to mimimize the cost of visiting n cities such
that we visit each city exactly once, and finishing
at the city where we start from?
“TRAVELING SALESMAN PROBLEM”
computer
network
link
Question
What is the shortest route to send a packet from A to
B?
“SHORTEST PATH PROBLEM”
web page
web link
module
prerequisite
Question
Find a sequence of modules to take that satisfy the
prerequisite requirements.
“TOPOLOGICAL SORT”
Other Applications
Biology
VLSI Layout
Vehicle Routing
Job Scheduling
Facility Location
:
:
Adjacency Matrix
double vertex[][];
1
23
4
1
-31 2 3
1 ∞ 4 -3
2 ∞ ∞ ∞
3 ∞ 1 ∞
Adjacency List
EdgeList vertex[];
1
23
4
1
-3
1
2
3
3 -3 2 4
2 1
neighbour cost
“Avoid Pointers in Competition..”
1
23
4
1
-3
1 2 3
2
3 2
1 4 -3
2
3 1
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
0
1
2
2
23
Level-Order on Treeif T is empty return
Q = new Queue
Q.enq(T)
while Q is not empty
curr = Q.deq()
print curr.element
if T.left is not empty
Q.enq(curr.left)
if curr.right is not empty
Q.enq(curr.right)
1
4 5
3
6
9 08
2
7
Calculating LevelQ = new Queue
Q.enq (v)
v.level = 0
while Q is not empty
curr = Q.deq()
if curr is not visited
mark curr as visited
foreach w in adj(curr)
if w is not visited
w.level = curr.level + 1
Q.enq(w)
A
C
D
B
EF
Search All Vertices
Search(G)
foreach vertex v
mark v as unvisited
foreach vertex v
if v is not visited
BFS(v)
A
C
D
B
EF
A
C
D
B
EF
A
C
D
B
EF
Applications
BFS
shortest path
DFS
longest path in DAG
finding connected component
detecting cycles
topological sort
Definition
A path on a graph G is a sequence of vertices v0, v1,
v2, .. vnwhere (vi,vi+1)∈E
The cost of a path is the sum of the cost of all
edges in the path. A
C
D
B
EF
A
C
D
B
EF
ShortestPath(s)
Run BFS(s)
w.level: shortest distance from s
w.parent: shortest path from s
A
C
D
B
EF
5
51
2
3
1
3
1
4
BFS(s) does not work
Must keep track of smallest distance so far.
If we found a new, shorter path, update the distance.
10
6
2
8
s w
v
Definition
distance(v) : shortest distance so far from s to v
parent(v) : previous node on the shortest path so far
from s to v
cost(u, v) : the cost of edge from u to v
10
6
2
8
s w
v
distance(w) = 8
cost(v,w) = 2
parent(w) = v
A
C
D
B
EF
5
51
2
3
1
3
1
4
0
8
8
8
8
8
5
51
2
3
1
3
1
4
0
5
8
8
8
8
5
51
2
3
1
3
1
4
0
5
8
8
8
8
5
51
2
3
1
3
1
4
0
5
10
8
6
8
5
51
2
3
1
3
1
4
0
5
10
8
6
8
5
51
2
3
1
3
1
4
0
5
8
8
610
5
51
2
3
1
3
1
4
0
5
8
8
610
5
51
2
3
1
3
1
4
0
5
8
8
610
5
51
2
3
1
3
1
4
0
5
8
8
610
5
51
2
3
1
3
1
4
0
5
8
8
610
5
1
2
3
4
Dijkstra’s Algorithm
color all vertices yellow
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
Dijkstra’s Algorithm
while there are yellow vertices
v = yellow vertex with min distance(v)
color v red
foreach neighbour w of v
relax(v,w)
Using Priority Queue
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
while pq is not empty
v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)
Initialization O(V)
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
Main Loop
while pq is not empty
v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)
0
8
8
8
8
8
5
31
-2
-3
1
3
1
-4
0
5
8
8
8
8
5
31
-2
-3
1
3
1
-4
0
5
8
2
6
8
5
31
-2
-3
1
3
1
-4
0
5
4
2
32
5
31
-2
-3
1
3
1
-4
0
5
1
2
3-1
5
31
-2
-3
1
3
1
-4
Bellman-Ford Algorithm
do |V|-1 times
foreach edge (u,v)
relax(u,v)
// check for negative weight cycle
foreach edge (u,v)
if distance(v) > distance(u) + cost(u,v)
ERROR: has negative cycle
Idea: Use DFS
Denote vertex as “visited”, “unvisited” and “visiting”
Mark a vertex as “visited” = vertex is finished
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
A
C
D
B
EF
unvisited
visiting
visited
unvisited
visiting
visited
Tree Edge
Backward Edge
Forward Edge
Cross Edge
(in a DAG)
A
C
D
B
EF
1 0
A
C
D
B
EF
unvisited
visiting
visited
1 02
A
C
D
B
EF
unvisited
visiting
visited
1 0
3
2
A
C
D
B
EF
unvisited
visiting
visited
1 0
3 4
5
2
Longest Path
Run DFS as usual
When a vertex v is finished, set
L(v) = max {L(u) + 1} for all edge (v,u)
Topological Sort
Goal: Order the vertices, such that if there is a path
from u to v, u appears before v in the output.
Topological Sort
 ACBEFD
 ACBEDF
 ACDBEF
A
C
D
B
EF
Topological Sort
Run DFS as usual
When a vertex is finish, push it onto a stack

More Related Content

What's hot

introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
Chuckie Balbuena
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 

What's hot (20)

Graph theory
Graph theoryGraph theory
Graph theory
 
Graph Theory: Trees
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Graph theory in network system
Graph theory in network systemGraph theory in network system
Graph theory in network system
 
Backtracking
BacktrackingBacktracking
Backtracking
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 

Similar to Directed Acyclic Graph

All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 

Similar to Directed Acyclic Graph (20)

Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Graph
GraphGraph
Graph
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Graphs
GraphsGraphs
Graphs
 
What is the distance between the points B and C Experience Tradition/tutorial...
What is the distance between the points B and C Experience Tradition/tutorial...What is the distance between the points B and C Experience Tradition/tutorial...
What is the distance between the points B and C Experience Tradition/tutorial...
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
 
Graph theory
Graph theoryGraph theory
Graph theory
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
2-Vector.pptx
2-Vector.pptx2-Vector.pptx
2-Vector.pptx
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 

More from AJAL A J

More from AJAL A J (20)

KEAM KERALA ENTRANCE EXAM
KEAM KERALA ENTRANCE EXAMKEAM KERALA ENTRANCE EXAM
KEAM KERALA ENTRANCE EXAM
 
Paleontology Career
Paleontology  CareerPaleontology  Career
Paleontology Career
 
CHEMISTRY basic concepts of chemistry
CHEMISTRY  basic concepts of chemistryCHEMISTRY  basic concepts of chemistry
CHEMISTRY basic concepts of chemistry
 
Ecology
EcologyEcology
Ecology
 
Biogeochemical cycles
Biogeochemical cyclesBiogeochemical cycles
Biogeochemical cycles
 
ac dc bridges
ac dc bridgesac dc bridges
ac dc bridges
 
Hays bridge schering bridge wien bridge
Hays bridge  schering bridge  wien bridgeHays bridge  schering bridge  wien bridge
Hays bridge schering bridge wien bridge
 
App Naming Tip
App Naming TipApp Naming Tip
App Naming Tip
 
flora and fauna of himachal pradesh and kerala
flora and fauna of himachal pradesh and keralaflora and fauna of himachal pradesh and kerala
flora and fauna of himachal pradesh and kerala
 
B.Sc Cardiovascular Technology(CVT)
 B.Sc Cardiovascular Technology(CVT)  B.Sc Cardiovascular Technology(CVT)
B.Sc Cardiovascular Technology(CVT)
 
11 business strategies to make profit
11 business strategies to make profit 11 business strategies to make profit
11 business strategies to make profit
 
PCOS Polycystic Ovary Syndrome
PCOS  Polycystic Ovary SyndromePCOS  Polycystic Ovary Syndrome
PCOS Polycystic Ovary Syndrome
 
Courses and Career Options after Class 12 in Humanities
Courses and Career Options after Class 12 in HumanitiesCourses and Career Options after Class 12 in Humanities
Courses and Career Options after Class 12 in Humanities
 
MANAGEMENT Stories
 MANAGEMENT Stories MANAGEMENT Stories
MANAGEMENT Stories
 
NEET PREPRATION TIPS AND STRATEGY
NEET PREPRATION TIPS AND STRATEGYNEET PREPRATION TIPS AND STRATEGY
NEET PREPRATION TIPS AND STRATEGY
 
REVOLUTIONS IN AGRICULTURE
REVOLUTIONS IN AGRICULTUREREVOLUTIONS IN AGRICULTURE
REVOLUTIONS IN AGRICULTURE
 
NRI QUOTA IN NIT'S
NRI QUOTA IN NIT'S NRI QUOTA IN NIT'S
NRI QUOTA IN NIT'S
 
Subjects to study if you want to work for a charity
Subjects to study if you want to work for a charitySubjects to study if you want to work for a charity
Subjects to study if you want to work for a charity
 
IIT JEE A KERALA PERSPECTIVE
IIT JEE A KERALA PERSPECTIVE IIT JEE A KERALA PERSPECTIVE
IIT JEE A KERALA PERSPECTIVE
 
Clat 2020 exam COMPLETE DETAILS
Clat 2020 exam COMPLETE DETAILSClat 2020 exam COMPLETE DETAILS
Clat 2020 exam COMPLETE DETAILS
 

Recently uploaded

Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

Directed Acyclic Graph

Editor's Notes

  1. A graph consists of a set of vertices and a set of edges between the vertices. In a tree, there is a unique path between any two nodes. In a graph, there may be more than one path between two nodes.
  2. In a weighted graph, edges have a weight (or cost) associated with it. Not all weights are labeled in this slides for simplicity.
  3. In an undirected graph, edges are bidirectional.
  4. In a complete graph, a node is connected to every other nodes. The number of edges in a complete graph is n(n-1)/2, where n is the number of vertices. (Why is it so?). Therefore, the number of edges is O(n2). A complete graph is also called a clique.
  5. A path is a sequence of vertices v0, v1, v2, .. vn where there is an edge between vi and vi+1. The length of a path p is the number of edges in p.
  6. A path v0, v1, v2, … vn is a cycle if vn = v0 and its length is at least 2. Note that the definition of path and cycle applies to directed graph as well.
  7. A path v0, v1, v2, … vn is a cycle if vn = v0 and its length is at least 2. Note that the definition of path and cycle applies to directed graph as well.
  8. In an undirected graph, edges are bidirectional.
  9. A path v0, v1, v2, … vn is a cycle if vn = v0 and its length is at least 2. Note that the definition of path and cycle applies to directed graph as well.
  10. In a connected graph, there is a path between every nodes. A graph does not have to be connected. The above graph has two connected components.
  11. In a connected graph, there is a path between every nodes. A graph does not have to be connected. The above graph has two connected components.
  12. Interested students may refer to [CLR] Sec 5.4 for more precise definition of graph terminologies.
  13. This requires O(N2) memory, and is not suitable for sparse graph. (Only 1/3 of the above matrix contains useful information). How about undirected graph? How would you represent it?
  14. This requires only O(V + E) memory.
  15. This requires only O(V + E) memory.
  16. Given a source vertex, we like to start searching from that source. The idea of BFS is that we visit all vertices that are of distance i away from the source before we visit vertices that are of distance i+1 away from the source. The order of search is not unique and depends on the order of neighbours visited.
  17. After BFS, we get a tree rooted at the source node. Edges in the tree are edges that we followed during searching. We call this a BFS tree. Vertices in the figure are labeled with their distance from the source.
  18. Similarly, we can maintain the distance of a vertex from the source.
  19. BFS guarantees that if there is a path to a vertex v from the source, we can always visit v. But since some vertices maybe unreachable from the source, we can call BFS multiple times from multiple source.
  20. Idea for DFS is to go as deep as possible.
  21. In Single-source shortest path problem, we are given a vertex v, and we want to find the path with minimum cost to every other vertices.
  22. If a graph is unweighted, we can treat the cost of each edge as 1.
  23. The shostest path for an unweighted graph can be found using BFS. To get the shortest path from a source s to a vertex v, we just trace back the parent pointer from v back to s. The number of edges in the path is given by the level of a vertex in the BFS tree. (Why does BFS guarantee that the paths are shortest?)
  24. Next, we look at another version of the problem, where the cost of the edges are positive.
  25. Convince yourself the BFS does not solve our shortest path problem here. Distance here refers to the cost of the path, not the number of edges as in BFS.
  26. In the following figures, we label a node with the shortest distance discovered so far from the source. Here is the basic idea that will help us solve our shortest path problem. If the current shortest distance from s to w is 10, to v is 6, and the cost of edge (v,w) is 2, then we have discovered a shorter path from s to w (through v).
  27. Now we are ready to describe our single source, shortest path algorithm for graphs with positive weights. The algorithm is called Dijkstra’s algorithm.
  28. Since priority queue supports efficient minimum picking operation, we can use a priority queue here to improve the running time. Note that we no longer color vertices here. Yellow vertices in the previous pseudocode are now vertices that are in the priority queue.
  29. Initialization still takes O(V)
  30. But we have to be more careful with the analysis of the main loop. We know that each deleteMin() takes O(log V) time. But relax(v,w) is no longer O(1).
  31. I claim that the running time for Bellman-Ford algorithm is O(VE). Verify this claim.
  32. Idea for DFS is to go as deep as possible.
  33. ACDBEF is NOT topologically sorted.
  34. What is the running time for Toposort?