Upload
sharowar-jahan
View
22
Download
0
Embed Size (px)
DESCRIPTION
CSC2105 Graph Introduction
Citation preview
CSC2105: AlgorithmsCSC2105: AlgorithmsGraph - IntroductionGraph - Introduction
Mashiour RahmanMashiour [email protected]@aiub.edu
American International University BangladeshAmerican International University Bangladesh
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 2
Graph - IntroductionGraph - IntroductionReference:Reference:
• CLRS Appendix B.4, Chapters 22, 24-26
Objectives:Objectives:
• To learn several significant graph algorithms and their applications
a. Graph search (BFS, DFS)
b. Bellman/Ford and Dijkstra’s shortest path algorithms (Greedy)
c. Floyd’s algorithm (Dynamic Programming)
d. Network flows
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 3
MotivationMotivationState-space search in Artificial IntelligenceState-space search in Artificial Intelligence
Geographical information systems, electronic Geographical information systems, electronic street directorystreet directory
Logistics and supply chain managementLogistics and supply chain management
Telecommunications network designTelecommunications network design
Many more industry applicationsMany more industry applications
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 4
Review: GraphsReview: GraphsGraph Graph GG = ( = (VV, , EE))
V = {1,…n} = set of vertices, E = set of e edges
Undirected graph:
edge (u, v) = edge (v, u)
no self-loops
Directed graph (digraph):
edge (u, v) goes from vertex u to vertex v
Sparse graph:
e = O(n), dense otherwise
A weighted graph associates weights with either the edges or the vertices
Degree of a vertex v:
deg(v) = number of edges adjacent on v (in-degree and out-degree for directed graphs)
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 5
Examples of GraphsExamples of Graphs
Directed & Undirected graphs.Directed & Undirected graphs.
a)a) A directed graph A directed graph G= (V, E)G= (V, E), where , where VV = {1,2,3,4,5,6} and = {1,2,3,4,5,6} and EE={(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(5,4),(3,6)}. The edge (2,2) ={(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(5,4),(3,6)}. The edge (2,2) is self loop.is self loop.
b)b) An undirected graph An undirected graph G = (V,E)G = (V,E), where , where VV = {1,2,3,4,5,6} and = {1,2,3,4,5,6} and EE = {(1,2),(1,5),(2,5),(3,6)}. The vertex 4 is isolated.= {(1,2),(1,5),(2,5),(3,6)}. The vertex 4 is isolated.
c)c) The subgraph of the graph in part (a) induced by vertex set The subgraph of the graph in part (a) induced by vertex set {1,2,3,6}{1,2,3,6}
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 6
Review: GraphsReview: GraphsAcyclic Acyclic : if a graph contains no cycles: if a graph contains no cycles
DAG DAG : Directed acyclic graphs: Directed acyclic graphs
Connected Connected : if every vertex of a graph can : if every vertex of a graph can reachreach every other vertex, i.e., every pair of vertices is every other vertex, i.e., every pair of vertices is connected by a pathconnected by a path
Connected Components Connected Components : equivalence classes of : equivalence classes of vertices under “is reachable from” relationvertices under “is reachable from” relation
Strongly connected Strongly connected : every 2 vertices are reachable : every 2 vertices are reachable from each other (in a digraph)from each other (in a digraph)
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 7
Forests, DAG, ComponentsForests, DAG, Components
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 8
Review: Representing GraphsReview: Representing GraphsAdjacency matrixAdjacency matrix: represents a graph as : represents a graph as n n x x n n matrix matrix AA::
A[i, j] = 1 if edge (i, j) E (or weight of edge)
= 0 if edge (i, j) E
Storage requirements: O(n2)
A dense representation
But, can be very efficient for small graphs
Especially if store just one bit/edge
Undirected graph: only need half of matrix
Adjacency listAdjacency list: list of adjacent vertices: list of adjacent vertices
For each vertex v V, store a list of vertices adjacent to v
Storage requirements: O(n+e)
Good for large, sparse graphs (e.g., planar maps)
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 9
Review: Representing GraphsReview: Representing Graphs
Two representations of an undirected graph.
a) An undirected graph G having five vertices and seven edges.
b) An adjacency-list representation of G.
c) The adjacency-matrix representation of G.
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 10
Graph SearchingGraph SearchingGivenGiven: a graph : a graph GG = = (V, E)(V, E), directed or undirected, directed or undirected
GoalGoal: methodically explore every vertex and edge: methodically explore every vertex and edge
UltimatelyUltimately: build a : build a treetree on the graph on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not connected
Breadth-first searchBreadth-first search
Depth-first searchDepth-first search
Other variants: best-first, iterated deepening search, Other variants: best-first, iterated deepening search, etc.etc.
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 11
Depth-First Search (DFS)Depth-First Search (DFS)Explore “deeper” in the graph whenever possibleExplore “deeper” in the graph whenever possible
Edges are explored out of the most recently discovered vertex v that
still has unexplored edges (LIFO)
When all of v’s edges have been explored, backtrack to the vertex from
which v was discovered
computes 2 timestamps: d[ ] (discovered) and f[ ] (finished)
builds one or more depth-first tree(s) (depth-first forest)
Algorithm colors each vertexAlgorithm colors each vertex
WHITE: undiscovered
GRAY: discovered, in process
BLACK: finished, all adjacent vertices have been discovered
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 12
Depth-First Search: The CodeDepth-First Search: The Code DFS(G)DFS(G)
{{
for each vertex u V
color[u] = WHITE;
time = 0;
for each vertex u V
if (color[u] == WHITE)
DFS_Visit(u);
}}
DFS_Visit(u)DFS_Visit(u){{
color[u] = GREY;color[u] = GREY;time = time+1;time = time+1;d[u] = time; d[u] = time; // compute d[]// compute d[]
for each v adjacent to ufor each v adjacent to uif (color[v] == WHITE)if (color[v] == WHITE)
p[v]= u p[v]= u // build tree// build tree
DFS_Visit(v);DFS_Visit(v);color[u] = BLACK;color[u] = BLACK;time = time+1;time = time+1;f[u] = time; f[u] = time; // compute f[]// compute f[]
}}
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 13
DFS AnalysisDFS AnalysisRunning time of Running time of DFSDFS = = O(n+e)O(n+e)
DFS (excluding DFS_Visit) takes O(DFS (excluding DFS_Visit) takes O(nn) time) time
DFS_Visit:DFS_Visit:
DFS_Visit( v ) is called exactly once for each vertex v
During DFS_Visit( v ), adjacency list of v is scanned once
sum of lengths of adjacency lists = O(e)
This type of aggregate analysis is an informal This type of aggregate analysis is an informal example of example of amortized analysisamortized analysis
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 14
DFS Classification of EdgesDFS Classification of EdgesDFS can be used to classify edges of DFS can be used to classify edges of GG::
1. Tree edges: edges in the depth-first forest.
2. Back edges: edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree.
3. Forward edges: non-tree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree.
4. Cross edges: all other edges.
DFS yields valuable information about the DFS yields valuable information about the structurestructure of a graph.of a graph.
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 15
Operations of DFSOperations of DFS
x z y
w v u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 16
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 17
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 18
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 19
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 20
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 21
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/54/5
x z3/
y
w
2/
v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 22
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/54/5
x z3/63/6
y
w
2/
v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 23
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/54/5
x z3/63/6
y
w
2/72/7
v
1/
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 24
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/54/5
x z3/63/6
y
w
2/72/7
v
1/
u
Forward Edge
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 25
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/54/5
x z3/63/6
y
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
4/54/5
x z3/63/6
y
w
2/72/7
v
1/81/8
u
Forward Edge
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 26
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/54/5
x z3/63/6
y
9/
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 27
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
4/54/5
x z3/63/6
y
9/
w
2/72/7
v
1/81/8
u
Cross Edge
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 28
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
Cross Edge
4/54/5
x10/
z3/63/6
y
9/
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 29
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
Cross Edge
4/5
x10/
z3/6
y
9/
w
2/7
v
1/8
u
4/54/5
x10/
z3/63/6
y
9/
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 30
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
Cross Edge
4/5
x10/
z3/6
y
9/
w
2/7
v
1/8
u
4/5
x10/
z3/6
y
9/
w
2/7
v
1/8
u
4/54/5
x10/1110/11
z3/63/6
y
9/
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 31
Operations of DFSOperations of DFS
x z y
w v u
x z y
w v
1/
u
x z y
w
2/
v
1/
u
Tree edge
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
4/
x z3/
y
w
2/
v
1/
u
Back Edge
4/5
x z3/
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/
u
4/5
x z3/6
y
w
2/7
v
1/8
u
4/5
x z3/6
y
w
2/7
v
1/8
u
Forward Edge
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
4/5
x z3/6
y
9/
w
2/7
v
1/8
u
Cross Edge
4/5
x10/
z3/6
y
9/
w
2/7
v
1/8
u
4/5
x10/
z3/6
y
9/
w
2/7
v
1/8
u
4/5
x10/11
z3/6
y
9/
w
2/7
v
1/8
u
4/54/5
x10/1110/11
z3/63/6
y
9/129/12
w
2/72/7
v
1/81/8
u
Undiscovered
Discovered, On Process
FinishedFinished, all all adjacent vertices adjacent vertices have been have been discovereddiscovered
Discover time/ Finish time
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 32
Cycle DetectionCycle DetectionTheorem: An undirected graph is Theorem: An undirected graph is acyclic acyclic iff a DFS iff a DFS yields no yields no back edgesback edges
ProofProof
If acyclic, no back edges by definition (because a back edge implies a cycle)
If no back edges, acyclic
No back edges implies only tree edges (Why?)
Only tree edges implies we have a tree or a forest
Which by definition is acyclic
Thus, can run DFS to find whether a graph has a Thus, can run DFS to find whether a graph has a cycle. cycle. How would you modify the code to detect How would you modify the code to detect cycles?cycles?
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 33
Directed Acyclic Graph (DAG)Directed Acyclic Graph (DAG)Arises in many applications where there are Arises in many applications where there are precedence or ordering constraints (e.g. scheduling precedence or ordering constraints (e.g. scheduling problems)problems)
if there are a series of tasks to be performed, and certain tasks must precede other tasks
In general, a precedence constraint graph is a DAG, In general, a precedence constraint graph is a DAG, in which vertices are tasks and edge (u, v) means that in which vertices are tasks and edge (u, v) means that task u must be completed before task v beginstask u must be completed before task v begins
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 34
Topological SortTopological SortFind a linear ordering of all vertices of the DAG such Find a linear ordering of all vertices of the DAG such that if G contains an edge (u, v), u appears before v that if G contains an edge (u, v), u appears before v in the ordering.in the ordering.
In general, there may be many legal topological In general, there may be many legal topological orders for a given DAG.orders for a given DAG.
IdeaIdea::
1. Call DFS(G) to compute finishing time f[ ]
2. Insert vertices onto a linked list according to decreasing order of f[ ]
How to modify DFS to perform Topological Sort in O(n+e) How to modify DFS to perform Topological Sort in O(n+e) time?time?
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 35
Strongly Connected ComponentsStrongly Connected ComponentsDigraphsDigraphs are often used to model communication and are often used to model communication and transportation networks transportation networks
People want to know that the networks are complete in the People want to know that the networks are complete in the sense that from any location it is possible to reach another sense that from any location it is possible to reach another location in the digraphlocation in the digraph
How to find strongly connected components (SCC) of a How to find strongly connected components (SCC) of a digraph?digraph?
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 36
AlgorithmAlgorithm (SCC) (SCC)Strongly-Connected-Components(G)Strongly-Connected-Components(G)
1. call DFS(G) to compute finish times f[]
2. compute GT
3. call DFS(GT) and consider vertices in decreasing f[] computed in step 1
4. output vertices of each tree in DFS(GT) as separate strongly connected component
Total running time Total running time ΘΘT(n + e)T(n + e)
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 37
Breadth-First Search (BFS)Breadth-First Search (BFS)
Given source vertex Given source vertex ss,,
systematically explore the breadth of the frontier to
discover every vertex reachable from s
computes the distance d[ ] from s to all reachable vertices builds a breadth-first tree rooted at s
AlgorithmAlgorithm
colors each vertex:
WHITE : undiscovered
GRAY: discovered, in process
BLACK: finished, all adjacent vertices have been discovered
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 39
BFS: The CodeBFS: The CodeBFS(G, s) {BFS(G, s) {
initialize vertices;initialize vertices;
Q = {s};Q = {s};
while (Q not empty) {while (Q not empty) {
u = Dequeue(Q);u = Dequeue(Q);
for each v adjacent to u do {for each v adjacent to u do {
if (color[v] == WHITE) {if (color[v] == WHITE) {
color[v] = GRAY;color[v] = GRAY;
d[v] = d[u] + 1;// compute d[]d[v] = d[u] + 1;// compute d[]
p[v] = u; // build BFS treep[v] = u; // build BFS tree
Enqueue(Q, v);Enqueue(Q, v);
}}
}}
color[u] = BLACK;color[u] = BLACK;
}}
}}
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 52
BFS Analysis BFS Analysis initializeinitialize : : O(n)O(n)
LoopLoop: Queue operations and Adjacency checks: Queue operations and Adjacency checks
Queue operationsQueue operations
each vertex is enqueued/dequeued at most once. Why?
each operation takes O(1) time, hence O(n)
Adjacency checksAdjacency checks
adjacency list of each vertex is scanned at most once
sum of lengths of adjacency lists = O(e)
Total run time of BFS = Total run time of BFS = O(n+e)O(n+e)
Mashiour Rahman
AIUB::CSC2105::Algorithms Graph Introduction 53
Breadth-First Search: Breadth-First Search: PropertiesProperties
What do we get the end of BFS?What do we get the end of BFS?
1.1. d[v]d[v] = = shortest-path distanceshortest-path distance from from ss to to vv, i.e. , i.e. minimum number of edges from minimum number of edges from ss to to vv, or , or ∞∞ if if v v not not reachable from reachable from ss
Proof : refer CLRS
2.2. a a breadth-firstbreadth-first tree tree, in which path from root , in which path from root ss to to any vertex any vertex vv represent a shortest path represent a shortest path
Thus can use BFS to calculate shortest path from one vertex to another in O(n+e) time, for unweighted graphs.