Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph, A,B,C,D,E are known as vertices.
CE100 Algorithms and Programming II
Graph Terminology
Edge
An edge is a connecting link between two vertices.
Edge is also known as Arc.
An edge is represented as
(startingVertex,endingVertex)
For example, in above graph the link between vertices A and B is represented as
(A,B)
CE100 Algorithms and Programming II
Graph Terminology
Edge
In example graph, there are 7 edges
(A,B),(A,C),(A,D),(B,D),(B,E),(C,D),(D,E)
CE100 Algorithms and Programming II
Graph Terminology
Edge
Edges are three types.
Undirected Edge
Directed Edge
Weighted Edge
CE100 Algorithms and Programming II
Graph Terminology
Edge
Undirected Edge
An undirected egde is a bidirectional edge. If there is undirected edge between vertices A and B then edge (A,B) is equal to edge (B,A)
CE100 Algorithms and Programming II
Graph Terminology
Edge
Directed Edge
A directed egde is a unidirectional edge. If there is directed edge between vertices A and B then edge (A,B) is not equal to edge (B,A).
CE100 Algorithms and Programming II
Graph Terminology
Edge
Weighted Edge
A weighted egde is a edge with value (cost) on it.
CE100 Algorithms and Programming II
Graph Terminology
Undirected Graph
A graph with only undirected edges is said to be undirected graph.
CE100 Algorithms and Programming II
Graph Terminology
Directed Graph
A graph with only directed edges is said to be directed graph.
CE100 Algorithms and Programming II
Graph Terminology
Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.
CE100 Algorithms and Programming II
Graph Terminology
End vertices or Endpoints
The two vertices joined by edge are called end vertices (or endpoints) of that edge.
In graph theory, a vertex with degree 1 is called an end vertex (plural end vertices)
CE100 Algorithms and Programming II
Graph Terminology
Origin
If a edge is directed, its first endpoint is said to be the origin of it.
CE100 Algorithms and Programming II
Graph Terminology
Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be the destination of that edge.
CE100 Algorithms and Programming II
Graph Terminology
Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other words, vertices A and B are said to be adjacent if there is an edge between them.
CE100 Algorithms and Programming II
Graph Terminology
Incident
Edge/Arc is said to be incident on a Vertex/Node if the Vertex/Node is one of the endpoints of that Edge/Arc.
An incidence is a pair (B,e1) where B is a vertex and e1 is an edge incident to B
Two distinct incidences (B,e1) and (v,e2) are adjacent if and only if B=v, e1=e2 or BB′=e1 or e2.
CE100 Algorithms and Programming II
Graph Terminology
Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
CE100 Algorithms and Programming II
Graph Terminology
Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.
CE100 Algorithms and Programming II
Graph Terminology
Degree
Total number of edges connected to a vertex is said to be degree of that vertex.
CE100 Algorithms and Programming II
Graph Terminology
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
CE100 Algorithms and Programming II
Graph Terminology
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
CE100 Algorithms and Programming II
Graph Terminology
Parallel edges or Multiple edges
If there are two undirected edges with same end vertices and two directed edges with same origin and destination, such edges are called parallel edges or multiple edges.
CE100 Algorithms and Programming II
Graph Terminology
Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.
CE100 Algorithms and Programming II
Graph Terminology
Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
CE100 Algorithms and Programming II
Graph Terminology
Complex Graph
A graph is said to be complex if there are parallel or self-loop edges.
CE100 Algorithms and Programming II
Graph Terminology
Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex such that each edge is incident to its predecessor and successor vertex.
CE100 Algorithms and Programming II
Graph Representations
CE100 Algorithms and Programming II
Graph Representations
Graph data structure is represented using following representations
Adjacency Matrix
Incidence Matrix
Adjacency List
CE100 Algorithms and Programming II
Graph Representations
Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices.
That means a graph with 4 vertices is represented using a matrix of size 4X4.
In this matrix, both rows and columns represent vertices.
This matrix is filled with either 1 or 0.
Here,
1 represents that there is an edge from row vertex to column vertex and
0 represents that there is no edge from row vertex to column vertex.
CE100 Algorithms and Programming II
Graph Representations
Adjacency Matrix
Undirected Graph
CE100 Algorithms and Programming II
Graph Representations
Adjacency Matrix
Directed Graph
CE100 Algorithms and Programming II
Graph Representations
Incidence Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a total number of edges.
That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6.
In this matrix, rows represent vertices and columns represents edges.
This matrix is filled with 0 or 1 or -1.
Here,
0 represents that the row edge is not connected to column vertex,
1 represents that the row edge is connected as the outgoing edge to column vertex and
-1 represents that the row edge is connected as the incoming edge to column vertex.
CE100 Algorithms and Programming II
Graph Representations
Incidence Matrix
CE100 Algorithms and Programming II
Graph Representations
Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.
CE100 Algorithms and Programming II
Graph Representations
Adjacency List
Linked List Implementation
CE100 Algorithms and Programming II
Graph Representations
Adjacency List
Reference Array Implementation
CE100 Algorithms and Programming II
Graph Representations - Review
CE100 Algorithms and Programming II
Graph Representations - Review
The standard two ways to represent a graph G=(V,E)
As a collection of adjacency-lists
As an adjacency-matrix
Adjacency-list representation is usually preferred
Provides a compact way to represent sparse graphs
Those graphs for which ∣E∣<<∣V∣2
CE100 Algorithms and Programming II
Graph Representations - Review
Adjacency-matrix representation may be preferred
for dense graphs for which ∣E∣ is close to ∣V∣2
when we need to be able to tell quickly if there is an edge connecting two given vertices
CE100 Algorithms and Programming II
Adjacency-List Representation - Review
An array Adj of ∣V∣ lists, one for each vertex u∈V
For each u∈V the adjacency-list Adj[u] contains (pointers to) all vertices v such that (u,v)∈E
That is, Adj[u] consists of all vertices adjacent to u in G
The vertices in each adjacency-list are stored in an arbitrary order
CE100 Algorithms and Programming II
Adjacency-List Representation - Review
If G is a directed graph
The sum of the lengths of the adjacency lists =∣E∣
If G is an undirected graph
The sum of the lengths of the adjacency lists =2∣E∣
since an edge (u,v) appears in both Adj[u] and Adj[v]
CE100 Algorithms and Programming II
Undirected Graphs Representations - Review
CE100 Algorithms and Programming II
Directed Graphs Representations - Review
CE100 Algorithms and Programming II
Adjacency List Representation (continued) - Review
Adjacency list representation has the desirable property
it requires O(max(V,E))=O(V+E) memory
for both undirected and directed graphs
Adjacency lists can be adopted to represent weighted graphs
each edge has an associated weight typically given by a weight functionw:E→R
The weight w(u,v) of an edge (u,v)∈E is simply stored with
vertex v in Adj[u] or with
vertex u in Adj[v] or both
CE100 Algorithms and Programming II
Adjacency List Representation (continued) - Review
A potential disadvantage of adjacency list representation
there is no quicker way to determine if a given edge (u,v) is present in G than to search v in Adj[u] or u in Adj[v]
This disadvantage can be remedied by an adjacency matrix representation at the cost of using asymptotically more memory
CE100 Algorithms and Programming II
Adjacency Matrix Representation - Review
Assume that, the vertices of G=(V,E) are numbered as 1,2,…,∣V∣
Adjacency matrix rep. consists of a ∣V∣×∣V∣ matrix A=(aij)∍
aij={10if(i,j)∈Eotherwise
Requires Θ(V2) memory independent of the number of edges ∣E∣
We define the transpose of a matrix A=(aij) to be the matrix
AT=(aij)T given by aijT=aji
Since in an undirected graph, (u,v) and (v,u) represent the same edge A=AT for an undirected graph
That is, adjacency matrix of an undirected graph is symmetric
Hence, in some applications, only upper triangular part is stored
CE100 Algorithms and Programming II
Adjacency Matrix Representation - Review
Adjacency matrix representation can also be used for
weighted graphs
aij={w(i,j)NILor0or∞if(i,j)∈Eotherwise
Adjacency matrix may also be preferable for
reasonably small graphs
Moreover, if the graph is unweighted
rather than using one word of memory for each matrix entry adjacency matrix representation uses one bit per entry
CE100 Algorithms and Programming II
Introduction to Graphs - Review
G=(V,E)
Adjency List Complexity O(degreeofu)(u,v)∈E
Sparse Matrix →∣E∣<∣V2∣
Dense Matrix →∣E∣closeto∣V2∣
Space Complexity Θ(∣V∣+∣E∣)
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Many definitions for directed and undirected graphs are the same although certain terms have slightly different meanings
If (u,v)∈E in a directed graphG=(V,E), we say that (u,v) is incident from or leaves vertex u and is incident to or enters vertex v
If (u,v)∈E in an undirected graphG=(V,E), we say that (u,v) is incident on vertices u and v
If (u,v) is an edge in a graph G=(V,E), we say that vertex v is adjacent to vertex u
When the graph is undirected,the adjacency relation is symmetric
When the graph is directed
the adjacency relation is not necessarily symmetric
if v is adjacent to u, we sometimes write u→v
CE100 Algorithms and Programming II
Introduction to Graphs - Review
The degree of a vertex in an undirected graph is the number of edges incident on it
In a directed graph,
out-degree of a vertex: number of edges leaving it
in-degree of a vertex: number of edges entering it
degree of a vertex: its in-degree + its out-degree
A path of lengthk from a vertex u to a vertex u′ in a graph G=(V,E) is a sequence⟨v0,v1,v2,…,vk⟩ of vertices such
that v0=u,vk=u′ and (vi−1,vi)∈E, for i=1,2,…,k
The length of a path is the number of edges in the path
CE100 Algorithms and Programming II
Introduction to Graphs - Review
If there is a path p from u to u′, we say that u′ is reachable from u via p:upu′
A path is simple if all vertices in the path are distinct
A subpath of path p=⟨v0,v1,v2,…,vk⟩ is a contiguous subsequence of its vertices
That is, for any 0≤i≤j≤k, the subsequence of vertices ⟨vi,vi+1,…,vj⟩ is a subpath of p
In a directed graph, a path ⟨v0,v1,…,vk⟩ forms a cycle if v0=vk and the path contains at least one edge
The cycle is simple if, in addition, v0,v1,…,vk are distinct
A self-loop is a cycle of length 1
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Two paths ⟨v0,v1,v2,…,vk⟩ & ⟨v0′,v1′,v2′,…,vk′⟩ form the same cycle if there is an integer j such that vi′=v(i+j)modk for i=0,1,…,k−1
The path p1=⟨1,2,4,1⟩ forms the same cycles as the paths
p2=⟨2,4,1,2⟩ and p3=⟨4,1,2,4⟩
A directed graph with no self-loops is simple
In an undirected graph a path ⟨v0,v1,…,vk⟩ forms a cycle
if v0=vk and v1,v2,…,vk are distinct
A graph with no cycles is acyclic
CE100 Algorithms and Programming II
Introduction to Graphs - Review
An undirected graph is connected
if every pair of vertices is connected by a path
The connected components of a graph are the
equivalence classes of vertices under the
"is reachable from" relation
An undirected graph is connected if it has exactly one component,
i.e., if every vertex is reachable from every other vertex
CE100 Algorithms and Programming II
Introduction to Graphs - Review
A directed graph is strongly-connected
if every two vertices are reachable from each other
The strongly-connected components of a digraph are the
equivalence classes of vertices under the
"are mutually reachable" relation
A directed graph is strongly-connected
if it has only one strongly-connected component
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Two graphs G=(V,E) and G′=(V′,E′) are isomorphic
if there exists a bijection f:V→V′ such that
(u,v)∈E⟺(f(u),f(v))∈E′
That is, we can relabel the vertices of G to be vertices of G′ maintaining the corresponding edges in G and G′
CE100 Algorithms and Programming II
Introduction to Graphs - Review
A graph G′=(V′,E′) is a subgraph of G=(V,E) if
V⊆V and E′⊆E
Given a set V′⊆V, the subgraph of Ginduced by V′ is the graph
G′=(V′,E′) where E′={(u,v)∈E:u,v∈V′}
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Given an undirected graph G=(V,E), the directed version of G is the directed graph G′=(V′,E′), where
(u,v)∈E′ and (v,u)∈E′⟺(u,v)∈E
That is, each undirected edge (u,v) in G is replaced in G′ by two directed edges (u,v) and (v,u)
Given a directed graph G=(V,E), the undirected version of G is the undirected graph G′=(V′,E′), where
(u,v)∈E′⟺u=v and (u,v)∈E
That is the undirected version contains the edges of G
"with their directions removed" and with self-loops eliminated
CE100 Algorithms and Programming II
Introduction to Graphs - Review
i.e., (u,v) and (v,u) in G are replaced in G′ by the same edge (u,v)
In a directed graph G=(V,E), a neighbor of a vertex u is any vertex that is adjacent to u in the undirected version of G
That, is v is a neighbor of u⟺ either (u,v)∈E or (v,u)∈E
v is a neighbor of u in both cases
In an undirected graph, u and v are neighbors if they are adjacent
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Several kinds of graphs are given special names
Complete graph: undirected graph in which every pair of vertices is adjacent
Bipartite graph: undirected graph G=(V,E) in which V can be partitioned into two disjoint sets V1 and V2 such that
(u,v)∈E implies either u∈V1 and v∈V2 or u∈V2 and v∈V1
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Forest: acyclic, undirected graph
Tree: connected, acyclic, undirected graph
Dag: directedacyclic graph
Multigraph: undirected graph with multiple edges between vertices and self-loops
CE100 Algorithms and Programming II
Introduction to Graphs - Review
Hypergraph: like an undirected graph, but each hyperedge,
rather than connecting two vertices,
connects an arbitrary subset of vertices
CE100 Algorithms and Programming II
Free Trees
CE100 Algorithms and Programming II
Free Trees
A free tree is a connected, acyclic, undirected graph
We often omit the adjective "free" when we say that a graph is a tree
If an undirected graph is acyclic but possibly disconnected it is a forest
CE100 Algorithms and Programming II
Theorem (Properties of Free Trees)
The following are equivalent for an undirected graph G=(V,E)
G is a free tree
Any two vertices in G are connected by a unique simple-path
G is connected, but if any edge is removed from E the resulting graph is disconnected
G is connected, and ∣E∣=∣V∣−1
G is acyclic, and ∣E∣=∣V∣−1
G is acyclic, but if any edge is added to E, the resulting graph contains a cycle
CE100 Algorithms and Programming II
Properties of Free Trees (1⇒2)
G is a free tree
Any two vertices in G are connected by a unique simple-path
CE100 Algorithms and Programming II
Properties of Free Trees (1⇒2)
Since a tree is connected, any two vertices in G are connected by a simple path
Let two vertices u,v∈V are connected by two simple paths p1 and p2
Let w and z be the first vertices at which p1 and p2 diverge and re-converge
Let p1′ be the subpath of p1 from w to z
Let p2′ be the subpath of p2 from w to z
p1′ and p2′ share no vertices except their end points
The path p1′∣∣p2′ is a cycle (contradiction)
CE100 Algorithms and Programming II
Properties of Free Trees (1⇒2)
p1′ and p2′ share no vertices except their end points
p1′∣∣p2′ is a cycle (contradiction)
Thus, if G is a tree, there can be at most one path between two vertices
CE100 Algorithms and Programming II
Properties of Free Trees (2⇒3)
Any two vertices in G are connected by a unique simple-path
G is connected, but if any edge is removed from E the resulting graph is disconnected
CE100 Algorithms and Programming II
Properties of Free Trees (2⇒3)
If any two vertices in G are connected by a unique simple path, then G is connected
Let (u,v) be any edge in E. This edge is a path from u to v. So it must be the unique path from u to v
Thus, if we remove (u,v) from G, there is no path from u to v
Hence, its removal disconnects G
CE100 Algorithms and Programming II
Properties of Free Trees (3⇒4)
Before proving 3⇒4 consider the following
Lemma: any connected, undirected graph G=(V,E)
satisfies ∣E∣≥∣V∣−1
Proof: Consider a graph G′ with ∣V∣ vertices and no edges.
Thus initially there are ∣C∣=∣V∣ connected components
Each isolated vertex is a connected component
Consider an edge (u,v) and let Cu and Cv denote the connected-components of u and v
CE100 Algorithms and Programming II
Properties of Free Trees (Lemma)$
If Cu=Cv then (u,v) connects Cu and Cv into a
connected component Cuv
Otherwise (u,v) adds an extra edge to the
connected component Cu=Cv
Hence, each edge added to the graph reduces the
number of connected components by at most 1
Thus, at least ∣V∣−1 edges are required to reduce the number of components to 1
Q.E.D
CE100 Algorithms and Programming II
Properties of Free Trees (3⇒4)
G is connected, but if any edge is removed from E the resulting graph is disconnected
G is connected, and ∣E∣=∣V∣−1
CE100 Algorithms and Programming II
Properties of Free Trees (3⇒4)
By assuming (3), the graph G is connected
We need to show both ∣E∣≥∣V∣−1 and ∣E∣≤∣V∣−1 in
order to show that ∣E∣=∣V∣−1
∣E∣≥∣V∣−1: valid due previous lemma
∣E∣≤∣V∣−1: (proof by induction)
Basis: a connected graph with n=1 or n=2 vertices has n−1 edges
IH: suppose that all graphs G′=(V′,E′) satisfying (3) also
satisfy ∣E′∣≤∣V′∣−1
CE100 Algorithms and Programming II
Properties of Free Trees (3⇒4)
Consider G=(V,E) that satisfies (3) with ∣V∣=n≥3
Removing an arbitrary edge (u,v) from G separates the graph into 2 connected graphs Gu=(Vu,Eu) and Gv=(Vv,Ev) such that V=Vu∪Vv and E=Eu∪Ev
Hence, connected graphs Gu and Gv both satisfy (3) else G would not satisfy (3)
Note that ∣Vu∣ and ∣Vv∣<n since ∣Vu∣+∣Vv∣=n
Hence, ∣Eu∣≤∣Vu∣−1 and ∣Ev∣≤∣Vv∣−1 (by IH)
Thus, ∣E∣=∣Eu∣+∣Ev∣+1≤(∣Vu∣−1)+(∣Vv∣−1)+1
⇒∣E∣≤∣V∣−1
Q.E.D
CE100 Algorithms and Programming II
Properties of Free Trees (4⇒5)
G is connected, and ∣E∣=∣V∣−1
G is acyclic, and ∣E∣=∣V∣−1
CE100 Algorithms and Programming II
Properties of Free Trees (4⇒5)
Suppose that G is connected, and ∣E∣=∣V∣−1, we must
show that G is acyclic
Suppose G has a cycle containing k vertices v1,v2,…,vk
Let Gk=(Vk,Ek) be subgraph of G consisting of the cycle
If k<∣V∣, there must be a vertex vk+1∈V−Vk that is adjacent to some vertex vi∈Vk, since G is connected
CE100 Algorithms and Programming II
Properties of Free Trees (4⇒5)
Define Gk+1=(Vk+1,Ek+1) to be subgraph of G with Vk+1=Vk∪vk+1 and Ek+1=Ek∪(vk+1,vi)
If k+1<∣V∣, we can similarly define Gk+2=(Vk+2,Ek+2) to be the subgraph of G with
Vk+2=Vk+1∪vk+2 and Ek+2=Ek+1∪(vk+2,vj)
for some vj∈Vk+1 where ∣Vk+2∣=∣Ek+2∣
CE100 Algorithms and Programming II
Properties of Free Trees (4⇒5)
We can continue defining Gk+m with ∣Vk+m∣=∣Ek+m∣ until we obtain Gn=(Vn,En) where
n=∣V∣ and Vn=∣V∣ and ∣Vn∣=∣En∣=∣V∣
Since Gn is a subgraph of G, we have
En⊆E⇒∣E∣≥∣En∣=∣V∣ which contradicts the assumption ∣E∣=∣V∣−1
Hence G is acyclic
Q.E.D
CE100 Algorithms and Programming II
Properties of Free Trees (5⇒6)
G is acyclic, and ∣E∣=∣V∣−1
G is acyclic, but if any edge is added to E, the resulting graph contains a cycle
CE100 Algorithms and Programming II
Properties of Free Trees (5⇒6)
Suppose that G is acyclic and ∣E∣=∣V∣−1
Let k be the number of connected components of G
G1=(V1,E1),G2=(V2,E2),…,Gk=(Vk,Ek) such that
i=1∪kVi=V;Vi∩Vj=∅;1≤i,j≤k and i=j
i=1∪kEi=E;Ei∩Ej=∅;1≤i,j≤k and i=j
Each connected component Gi is a tree by definition.
CE100 Algorithms and Programming II
Properties of Free Trees (5⇒6)
Since (1⇒5) each component Gi is satisfies
∣Ei∣=∣Vi∣−1 for i=1,2,…,k
Thus
i=1∑k∣Ei∣=i=1∑k∣Vi∣−i=1∑k1
∣E∣=∣V∣−k
Therefore, we must have k=1
CE100 Algorithms and Programming II
Properties of Free Trees (5⇒6)
That is (5)⇒G is connected ⇒G is a tree
Since (1⇒2)
any two vertices in G are connected by a unique simple path
Thus,
adding any edge to G creates a cycle
CE100 Algorithms and Programming II
Properties of Free Trees (6⇒1)
G is acyclic, but if any edge is added to E, the resulting graph contains a cycle
G is a free tree
CE100 Algorithms and Programming II
Properties of Free Trees (6⇒1)
Suppose that G is acyclic but if any edge is added to E a cycle is created
We must show that G is connected due to the definition
Let u and v be two arbitrary vertices in G
If u and v are not already adjacent
adding the edge (u,v) creates a cycle in
which all edges but (u,v) belong to G
CE100 Algorithms and Programming II
Properties of Free Trees (6⇒1)
Thus there is a path from u to v, and since u and v are chosen arbitrarily G is connected
Graphviz (short for Graph Visualization Software) is a package of open-source tools initiated by AT&T Labs Research for drawing graphs specified in DOT language scripts having the file name extension "gv". It also provides libraries for software applications to use the tools. Graphviz is free software licensed under the Eclipse Public License.
Graphviz consists of a graph description language named the DOT language[4] and a set of tools that can generate and/or process DOT files:
CE100 Algorithms and Programming II
Graphviz Layout Engines
dot
a command-line tool to produce layered drawings of directed graphs in a variety of output formats, such as (PostScript, PDF, SVG, annotated text and so on).
a graphical user interface to visualize and edit graphs.
CE100 Algorithms and Programming II
Graphviz Tools
lefty (DEPRECATED)
a programmable (in a language inspired by EZ[5]) widget that displays DOT graphs and allows the user to perform actions on them with the mouse. Therefore, Lefty can be used as the view in a model–view–controller GUI application that uses graphs.
PlantUML is an open-source tool allowing users to create diagrams from a plain text language. Besides various UML diagrams, PlantUML has support for various other software development related formats (such as Archimate, Block diagram, BPMN, C4, Computer network diagram, ERD, Gantt chart, Mind map, and WBD), as well as visualisation of JSON and YAML files.
Breadth-first search (BFS) is a graph traversal algorithm that starts at a vertex and explores as far as possible along each branch before backtracking.
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS)
Graph G=(V,E), directed or undirected with adjacency list repres.
GOAL: Systematically explores edges of G to
discover every vertex reachable from the source vertex s
compute the shortest path distance of every vertex
from the source vertex s
produce a breadth-first tree (BFT)Gπ with root s
BFT contains all vertices reachable from s
the unique path from any vertex v to s in G constitutes a shortest path from s to v in G
IDEA: Expanding frontier across the breadth -greedy-
propagate a wave 1 edge-distance at a time
using a FIFO queue:O(1) time to update pointers to both ends
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS)
Maintains the following fields for each u∈V
color[u]: color of u
WHITE : not discovered yet
GRAY : discovered and to be or being processed
BLACK : discovered and processed
π[u]: parent of u (NIL of u=s or u is not discovered yet)
In this algorithm, we use a queue to store the vertices that are yet to be visited.
Complexity of following part is O(V)
G -> Graph
s -> Source
BFS(G,s)// Mark all the vertices as not visitedfor each vertex u in G.V - {s}
u.color = white;
u.distance = infinity;
u.parent = NIL;
...
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS)
We enqueue the first vertex and mark it as visited.
...
WHILE Q is not empty
u = DEQUEUE(Q)
for each vertex v in G.Adj[u]
if v.color == white
v.color = gray;
v.distance = u.distance + 1;
v.parent = u;
ENQUEUE(Q, v)
u.color = black;
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS)
Complexity of BFS is O(V+E)=O(V)+O(E)+O(1)
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Complete Algorithm
G -> Graph
s -> Source
BFS(G,s)// Mark all the vertices as not visitedfor each vertex u in G.V - {s}
u.color = white;
u.distance = infinity;
u.parent = NIL;
s.color = gray;
s.distance = 0;
s.parent = NIL;
// Create a queue for BFS
Q = empty
ENQUEUE(Q, s)
WHILE Q is not empty
u = DEQUEUE(Q)
for each vertex v in G.Adj[u]
if v.color == white
v.color = gray;
v.distance = u.distance + 1;
v.parent = u;
ENQUEUE(Q, v)
u.color = black;
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
s is the source vertex.
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-1
//init the graph
s.parent = NIL;
s.color = gray;
s.distance = 0;
Q = empty;
ENQUEUE(Q, s)
and
u = DEQUEUE(Q) in the while loop
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-2
----------------
Q = {c,a}
s = b
----------------
c.parent = s
c.distance = 1
c.color = gray
----------------
a.parent = s
a.distance = 1
a.color = gray
----------------
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-3
----------------
Q = {e,c}
a = b
----------------
e.parent = a
e.distance = 2
e.color = gray
----------------
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-4
----------------
Q = {g,e}
c = b
----------------
g.parent = c
g.distance = 2
g.color = gray
----------------
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-5
----------------
Q = {b,h,g}
e = b
----------------
h.parent = e
h.distance = 3
h.color = gray
----------------
b.parent = e
b.distance = 3
b.color = gray
----------------
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-6
----------------
Q = {f,i,b,h}
g = b
----------------
i.parent = g
i.distance = 3
i.color = gray
----------------
f.parent = e
f.distance = 3
f.color = gray
----------------
CE100 Algorithms and Programming II
Graph Traversal
Breadth-first search (BFS) Example-1
STEP-7
----------------
Q = {f,i,b}
h = b
----------------
Complexity of the following part is Θ(V+V)=O(V) (two sequential loops)
DFS(G)
for each vertex u in G.V
u.color = white
u.parent = nil
time = 0for each vertex u in G.V
if u.color == white
DFS-VISIT(G,u)
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Algorithm
DFS-VISIT(G,u)
time = time + 1
u.discovery = time
u.color = gray
for each vertex v in G.Adj[u]
if v.color == white
v.parent = u
DFS-VISIT(G,v)
u.color = black
time = time + 1
u.finish = time
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS)
DFS complexity is Θ(V+E)
Note for all v→v.discovery<v.finish
1≤u.discovery<u.finish≤2∣V∣
CE100 Algorithms and Programming II
Graph Traversal
Edge Classification in a DFF
Edge Types in DFS
Tree Edges
Back Edges
Forward Edges
Cross Edges
Colors in DFS
White -> Tree Edges
Gray -> Back Edges
Black -> Forward Edges
CE100 Algorithms and Programming II
Graph Traversal
Edge Classification in a DFF
Tree Edge: discover a new (WHITE) vertex
GRAY⇒WHITE
Back Edge: from a descendent to an ancestor in DFT
GRAY⇒GRAY
Forward Edge: from ancestor to descendent in DFT
GRAY⇒BLACK
Cross Edge: remaining edges (btwn trees and subtrees)
GRAY⇒BLACK
Note: ancestor/descendent is wrt Tree Edges
CE100 Algorithms and Programming II
Graph Traversal
Edge Classification in a DFF
How to decide which GRAY to BLACK edges are forward, which are cross
Let BLACK vertex v∈Adj[u] is encountered while processing GRAY vertex u
(u,v) is a forward edge if d[u]<d[v]
(u,v) is a cross edge if d[u]<d[v]
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-1
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-2
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-3
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-4
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-5
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-6
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-7
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-8
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-9
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-10
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-11
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-12
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-13
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-14
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
STEP-15
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
FINAL STEP-16
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-1
Edges and Clusters after DFS
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS) Example-2
Different Start Point and Different Graph
CE100 Algorithms and Programming II
Graph Traversal
Depth-first search (DFS)
Running time: Θ(V+E)
Initialization loop in DFS : Θ(V)
Main loop in DFS: Θ(V) exclusive of time to execute calls to DFS-VISIT
DFS-VISIT is called exactly once for each v∈V since
DFS-VISIT is invoked only on white vertices and
DFS-VISIT(G,u) immediately colors u as gray
For loop of DFS-VISIT(G,u) is executed ∣Adj[u]∣ time
Since ∑∣Adj[u]∣=E, total cost of executing loop of
DFS-VISIT is Θ(E)
CE100 Algorithms and Programming II
Elementary Graph Algorithms
Depth-first search (DFS) Algorithm Summary
Step 1 - Define a Stack of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the stack.
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph
CE100 Algorithms and Programming II
Begining−of−DFS−Proof
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS: Parenthesis Theorem
Thm: In any DFS of G=(V,E), let int[v]=[d[v],f[v]] then exactly one of the following holds
for any u and v∈V
int[u] and int[v] are entirely disjoint
int[v] is entirely contained in int[u] and v is a descendant of u in a DFT
int[u] is entirely contained in int[v] and u is a descendant of v in a DFT
CE100 Algorithms and Programming II
Elementary Graph Algorithms
Parenthesis Thm (proof for the case d[u]<d[v])
Subcased[v]<f[u] (int[u] and int[v] are overlapping)
v was discovered while u was still GRAY
This implies that v is a descendant of u
So search returns back to u and finishes u after finishing v
i.e., d[v]<f[u]⇒int[v] is entirely contained in int[u]
Subcased[v]>f[u]⇒int[v] and int[u] are entirely disjoint
Proof for the case d[v]<d[u] is similar (dual) Q.E.D
CE100 Algorithms and Programming II
Elementary Graph Algorithms
Nesting of Descendents’ Intervals
Corollary 1 (Nesting of Descendents’ Intervals):
v is a descendant of u if and only if
d[u]<d[v]<f[v]<f[u]
Proof: immediate from the Parenthesis Thrm Q.E.D
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS Parenthesis Theorem
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS on Undirected Graphs
Ambiguity in edge classification, since (u,v) and (v,u) are the same edge
First classification is valid (whichever of (u,v) or (v,u) is explored first) Lemma 1: any DFS on an undirected graph produces only Tree and Backedges
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS on Undirected Graphs - Lemma 1: Proof
Assume (x,z) is a F(F?) (Figure-1)
But (x,z) must be a B, since DFS must finish z before resuming x
Assume (u,v) is a C(C?) btw subtrees (Figure-2-4)
But (y,u)&(y,v) cannot be both T; one must be a B and (u,v) must be a T
If (u,v) is first explored while processing u/v,(y,v)/(y,u) must be a B (Figure-2-4)
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS on Undirected Graphs
Lemma 2: an undirected graph is acyclic (i.e. a forest) iff DFS yields no Backedges
Proof
(acyclic ⇒ no Back edges; by contradiction):
Let (u,v) be a B then color[u]=color[v]=GRAY
⇒ there exists a path between u and v
So, (u,v) will complete a cycle (Backedge⇒cycle)
(noBackedges⇒acyclic):
If there are no Backedges then there are only T edges by
Lemma 1⇒ forest \Rightarrow acyclic Q.E.D
CE100 Algorithms and Programming II
Elementary Graph Algorithms
DFS on Undirected Graphs (Cycle Detection)
How to determine whether an undirected graph G=(V,E) is acyclic
Run a DFS on G:
if a Backedge is found then there is a cycle
Running time: O(V), not O(V+E)
If ever seen ∣V∣ distinct edges,
must have seen a back edge (∣E∣≤∣V∣−1 in a forest)
CE100 Algorithms and Programming II
DFS: White Path Theorem
WPT: In a DFS of G, v is a descendent of u iff at time d[u], v can be reached from u along a WHITE path
Proof (⇒): assume v is a descendent of u
Let w be any vertex on the path from u to v in the DFT
So, w is a descendent of u⇒d[u]<d[w]
(by Corollary 1 nesting of descendents’ intervals)
Hence, w is white at time d[u]
CE100 Algorithms and Programming II
DFS: White Path Theorem
Proof (⇐) assume a white path p(u,v) at time d[u] but v does not become a descendent of u in the DFT (contradiction):
Assume every other vertex along p becomes a descendent of u in the DFT
CE100 Algorithms and Programming II
DFS: White Path Theorem
otherwise let v be the closest vertex to u along p that does
not become a descendent
Let w be predecessor of v along p(u,v):
d[u]<d[w]<f[w]<f[u] by Corollary 1
Since, v was WHITE at time d[u] (u was GRAY) d[u]<d[v]
Since, w is a descendent of u but v is not
d[w]<d[v]⇒d[v]<f[w]
By (1)–(3): d[u]<d[v]<f[w]<f[u]⇒d[u]<d[v]<f[w]
So by Parenthesis Thm int[v] is within int[u], v is descendent of u
1- call DFS(G) compute all u.finishTime values
2- compute G^T = (V,E^T) and reverse edge directions
3- call DFS(G^T) but in the main loop,
consider vertices
in order of decreasing u.finishTime(as computed in DFS)
CE100 Algorithms and Programming II
Graph Segmentation - SCC - Kosaraju's algorithm
for each unvisited vertex u, DFS(u)
try all free neighbor v of u, DFS(v)
finish DFS(u), add u to the front of list
transpose the graph
DFS in order of the list, DFS(u)try all free neighbor v of u, DFS(v)
each time we complete a DFS, we get an SCC
CE100 Algorithms and Programming II
Graph Segmentation - SCC - Tarjan's algorithm
for each unvisited vertex u
DFS(u), s.push(u), num[u] = low[u] = DFSCount
for each neighbor v of u
if v is unvisited, DFS(v)
low[u] = min(low[u], low[v])
if low[u] == num[u] // root of an SCC
pop from stack s until we get u
Lemma 1: no path between a pair of vertices in the same SCC, ever leaves the SCC
Proof: let u and v be in the same SCC⇒u⇋v
let w be on some path u↦w↦v⇒u↦w
but v↦u⇒∃ a path w↦v↦u⇒w↦u
therefore u and w are in the same SCC⟹(Q.E.D)
CE100 Algorithms and Programming II
Strongly Connected Components
Theorem 1: in any DFS, all vertices in the same SCC are placed in the same DFT
Proof: let r be the first vertex discovered in SCCSr
because r is first, color[x]=WHITE∀x∈Sr−r at time d[r]
So all vertices are WHITE on each r↦x path ∀x∈Sr−r
since these paths never leave Sr
Hence each vertex in Sr−r becomes a descendent of r (White-path Theorem) ⟹ (Q.E.D)
CE100 Algorithms and Programming II
Notation for the Strongly Connected Components
d[u] and f[u] refer to those values computed by DFS(G) at step (1)
u↦v refers to G not GT
Definition: forefather ϕ(u) of vertex u
ϕ(u)= That vertex w such that u↦w and f[w] is maximized
ϕ(u)=u possible because u↦u⇒f[u]≤f[ϕ(u)]
CE100 Algorithms and Programming II
Strongly Connected Components
Lemma 2: ϕ(ϕ(u))=ϕ(u)
Proof try to show that f[ϕ(ϕ(u))]=f[ϕ(u)]:
For any u,v∈V;u↦v⇒Rv⊆Ru⇒f[ϕ(v)]≤f[ϕ(u)]
So, u↦ϕ(u)⇒f[ϕ(ϕ(u))]≤f[ϕ(u)]
Due to definition of ϕ(u) we have f[ϕ(ϕ(u))]≥f[ϕ(u)]
Therefore f[ϕ(ϕ(u))]=f[ϕ(u)]
f[x]=f[y]⇒x=y (same vertex)
CE100 Algorithms and Programming II
Strongly Connected Components
Properties of forefather:
Every vertex in an SCC has the same forefather which is in the SCC
Forefather of an SCC is the representative vertex of the SCC
In the DFS of G, forefather of an SCC is the
first vertex discovered in the SCC
last vertex finished in the SCC
CE100 Algorithms and Programming II
Strongly Connected Components
Theorem 2: ϕ(u) of any u∈V in any DFS of G is an ancestor of u
Proof: Trivial if ϕ(u)=u.
If ϕ(u)=u, consider color of ϕ(u) at time d[u]
ϕ(u) is GRAY: ϕ(u) is an ancestor of u⇒ proving the theorem
ϕ(u) is BLACK: f[ϕ(u)]<f[u]⇒ contradiction to def. of ϕ(u)
ϕ(u) is WHITE: exist2 cases according to colors of intermediate vertices on p(u,ϕ(u))
Path p(u,ϕ(u)) at time d[u]:
CE100 Algorithms and Programming II
Strongly Connected Components
Case 1: every intermediate vertex xi∈p(u,ϕ(u)) is WHITE
⇒ϕ(u) becomes a descendant of u (White−Path−Theorem)
⇒f[ϕ(u)]<f[u]
⇒ contradiction
Case 2: ∃ some non−WHITE intermediate vertices on p(u,ϕ(u))
Let xt be the last non−WHITE vertex on
p(u,ϕ(u))=⟨u,x1,x2,…,xr,ϕ(u)⟩
Then, xt must be GRAY since BLACK−to−WHITE edge (xt,xt+1) cannot exist
But then, p(xt,ϕ(u))=⟨xt+1,xt+2,…,xr,ϕ(u)⟩ is a white path
⇒ϕ(u) is a descendant of xt (by white-path theorem)
f[xt]>f[ϕ(u)]
contradicting our choice for ϕ(u)⟹Q.E.D.
CE100 Algorithms and Programming II
Strongly Connected Components
C1: in any DFS of G=(V,E) vertices u and ϕ(u) lie in the same SCC, ∀u∈V
Proof: u↦ϕ(u) (by definition) and ϕ(u)↦u since ϕ(u) is an ancestor of u (by Theorem 2)
Theorem 3: two vertices u,v∈V lie in the same SCC⟺ϕ(u)=ϕ(v) in a DFS of G=(V,E)
Proof: let u and v be in the same SCCCuv⇒u⇋v
CE100 Algorithms and Programming II
Strongly Connected Components
∀w:v↦w⇒u↦w and ∀w:u↦w⇒v↦w, i.e.,
every vertex reachable from u is reachable from v and vice-versa
So, w=ϕ(u)⇒w=ϕ(v) and w=ϕ(v)⇒w=ϕ(u) by definition of forefather
Proof: Let ϕ(u)=ϕ(v)=w∈Cw⇒u∈Cw by C1 and v∈Cw by C1
By Theorem 3: SCCs are sets of vertices with the same forefather
By Theorem 2 and parenthesis Theorem: A forefather is the first vertex discovered and the last vertex finished in its SCC
CE100 Algorithms and Programming II
SCC: Why do we Run DFS on GT?
Consider r∈V with largest finishing time computed by DFS on G
r must be a forefather by definition since r↦r and f[r] is maximum in V
Cr=?:Cr= vertices in r’s SCC={u∈V:ϕ(u)=r}
⇒Cr={u∈V:u↦randf[x]≤f[r]∀x∈Ru}
where Ru={v∈V:u↦v}
⇒Cr={u∈V:u↦r} since f[r] is maximum
⇒Cr=RrT={u∈V:r↦u∈GT}=reachability set of r∈GT
i.e., Cr= those vertices reachable from r∈GT
Thus DFS-VISIT(GT,r) identifies all vertices in Cr and
blackens them
CE100 Algorithms and Programming II
SCC: Why do we Run DFS on GT?
BFS(GT,r) can also be used to identify Cr
Then, DFS on GT continues with $DFS-VISIT(G^T, r')
where f[r′]>f[w]∀w∈V−Cr
r must be a forefather by definition since r′↦r′ and
f[r′] is maximum in V−Cr
CE100 Algorithms and Programming II
SCC: Why do we Run DFS on GT?
Hence by similar reasoning DFS-VISIT(GT,r′) identifies Cr′
Thus, each DFS-VISIT(GT,x) in DFS(GT)
identifies an SCCCx with ϕ=x
CE100 Algorithms and Programming II
End−of−SCC−Proof
CE100 Algorithms and Programming II
Directed Acyclic Graphs (DAG)
CE100 Algorithms and Programming II
Directed Acyclic Graphs (DAG)
No Directed Cycles
CE100 Algorithms and Programming II
Directed Acyclic Graphs (DAG)
Theorem: a directed graph G is acyclic iff DFS on G yields no Back edges
Proof (acyclic no Back edges; by contradiction):
Let (v,u) be a Back edge visited during scanning Adj[v]
⇒color[v]=color[u]=GRAY and d[u]<d[v]
⇒int[v] is contained in int[u]⇒v is descendent of u
⇒∃ a path from u to v in a DFT and hence in G
∴ edge (v,u) will create a cycle (Back edge ⇒ cycle)
CE100 Algorithms and Programming II
Directed Acyclic Graphs (DAG) - aAcyclic iff no Back edges
Proof (no Back edges ⇒ acyclic):
Suppose G contains a cycle C (Show that a DFS on G yields a BackEdge; proof by contradiction)
Let v be the first vertex discovered in C and let (u,v) be proceeding edge in C
At time d[v]:∃ a white path from v to u along C
By WhitePath Thrm u becomes a descendent of v in a DFT
Therefore (u,v) is a BackEdge (descendent to ancestor)
CE100 Algorithms and Programming II
Topological Sort of a DAG
CE100 Algorithms and Programming II
Graph Traversal - Topological Sort of a DAG
When we are scheduling jobs or tasks, they may have dependencies.
For example, before we finish task a, we have to finish b first.
In this case, given a set of tasks and their dependencies, how shall we arrange our schedules? There comes an interesting graph algorithm: Topological Sort.
According to Introduction to Algorithms, given a directed acyclic graph (DAG),
a topological sort is a linear ordering of all vertices such that for any edge (u, v), u comes before v.
Another way to describe it is that when you put all vertices horizontally on a line, all of the edges are pointing from left to right.
CE100 Algorithms and Programming II
Graph Traversal - Topological Sort of a DAG
Topological sort is a linear ordering of a directed acyclic graph.
If a graph has a cycle, it is not a directed acyclic graph.
A graph is acyclic if it has no cycles.
Linear ordering "<" of V such that
(u,v)∈E⇒u<v in ordering
Ordering may not be unique
i.e., mapping the partial ordering to total ordering may yield more than one orderings
CE100 Algorithms and Programming II
Graph Traversal -Topological Sort of a DAG
DFS version
The key observation is that, leaf nodes should always come after their parents and ancestors. Following this intuition we can apply DFS and output nodes from leaves to the root.
We need to implement a boolean array visited so that visited[i] indicates if we have visited vertex i.
For each unvisited node, we would first mark it as visited and call DFS() to start searching its neighbours.
After finishing this, we can insert it to the front of a list. After visiting all nodes, we can return that list.
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
DFS version
run DFS(G)
when a vertex finished, output it
vertices output in **reverse** topologically sorted order
Runs in O(V+E) time
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
DFS version
def topological_sort():
for each node:
if visited[node] is False:
dfs(node)
def dfs(node):
visited[node] = True
for nei in neighbours[node]:
dfs(node)
ifvisited(node)= false:
ret.insert_at_the_front(node)
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
DFS version
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-1
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-2
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-3
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-4
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-5
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-6
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-7
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-8
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-9
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version STEP-10
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - DFS Version
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
BFS version (Kahn's algorithm)
For BFS, we need an array indegree to keep the track of indegrees. Then we will try to output all nodes with 0 indegree, and remove the edges coming out of them at the same time. Besides, remember to put the nodes that become 0 indegree in the queue.
Then, we can keep doing this until all nodes are visited. To implement it, we can store the graph in an adjacent list (a hashmap or a dictionary in Python) and a queue to loop.
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
BFS version (Kahn's algorithm)
indegree = an array indicating indegrees for each node
neighbours = a HashMap recording neighbours of each node
queue = []
for i in indegree:
if indegree[i] == 0:
queue.append(i)
while !queue.empty():
node = queue.dequeue()
for neighbour in neighbours[node]:
indegree[neighbour] -= 1if indegree[neighbour] == 0:
queue.append(neighbour)
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-1
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-2
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-3
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-4
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-5
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-6
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-7
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-8
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-9
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-10
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort - BFS version (Kahn's algorithm)
STEP-11 (Final)
CE100 Algorithms and Programming II
Graph Traversal
Topological Sort of a DAG
Correctness of the Algorithm
Claim: (u,v)∈E⇒f[u]>f[v]
Proof: consider any edge (u,v) explored by DFS
when (u,v) is explored, u is GRAY
if v is GRAY, (u,v) is a Back edge (contradicting acyclic theorem)
if v is WHITE, v becomes a descendent of u (b WPT) ⇒f[v]<f[u]
if v is BLACK, f[v]<d[u]⇒f[v]<f[u] Q.E.D
CE100 Algorithms and Programming II
Topological Sort of a DAG - Getting Dressed Example
CE100 Algorithms and Programming II
Cycle Detection
CE100 Algorithms and Programming II
Detect Cycle in a Directed Graph
Approach:
Depth First Traversal can be used to detect a cycle in a Graph.
DFS for a connected graph produces a tree.
There is a cycle in a graph only if there is a back edge present in the graph.
A back edge is an edge that is
from a node to itself (self-loop) or
one of its ancestors in the tree produced by DFS.
CE100 Algorithms and Programming II
Detect Cycle in a Directed Graph
Algorithm:
Create the graph using the given number of edges and vertices.
Create a recursive function that initializes the current index or vertex, visited, and recursion stack.
Mark the current node as visited and also mark the index in recursion stack.
Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, If the recursive function returns true, return true.
If the adjacent vertices are already marked in the recursion stack then return true.
Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true return true. Else if for all vertices the function returns false return false.
CE100 Algorithms and Programming II
Detect Cycle in a Directed Graph
Complexity Analysis:
Time Complexity: O(V+E).
Time Complexity of this method is same as time complexity of DFS traversal which is O(V+E).
Space Complexity: O(V).
To store the visited and recursion stack O(V) space is needed.
CE100 Algorithms and Programming II
Detect cycle in an undirected graph
Approach:
Run a DFS from every unvisited node.
Depth First Traversal can be used to detect a cycle in a Graph.
DFS for a connected graph produces a tree.
There is a cycle in a graph only if there is a back edge present in the graph.
A back edge is an edge that is joining a node to
itself (self-loop) or
one of its ancestor in the tree produced by DFS.
To find the back edge to any of its ancestors
keep a visited array and if there is a back edge to any visited node
then there is a loop and return true.
CE100 Algorithms and Programming II
Detect cycle in an undirected graph
Algorithm:
Create the graph using the given number of edges and vertices.
Create a recursive function that have current index or vertex, visited array and parent node.
Mark the current node as visited .
Find all the vertices which are not visited and are adjacent to the current node.
Recursively call the function for those vertices, If the recursive function returns true return true.
If the adjacent node is not parent and already visited then return true.
Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true, return true.
Else if for all vertices the function returns false return false.
CE100 Algorithms and Programming II
Detect cycle in an undirected graph
Complexity Analysis:
Time Complexity: O(V+E).
The program does a simple DFS Traversal of the graph which is represented using adjacency list. So the time complexity is O(V+E).
Space Complexity: O(V).
To store the visited array O(V) space is required.
CE100 Algorithms and Programming II
Graph Coloring
CE100 Algorithms and Programming II
Graph Coloring
Given an undirected graph and a number m,
determine if the graph can be coloured with at most m colours such that no two adjacent vertices of the graph are colored with the same color.
Here coloring of a graph means the assignment of colors to all vertices.
CE100 Algorithms and Programming II
Graph Coloring
Naive Approach:
Generate all possible configurations of colors.
Since each node can be coloured using any of the m available colours,
the total number of colour configurations possible are mV.
After generating a configuration of colour,
check if the adjacent vertices have the
same colour or not.
If the conditions are met,
print the combination and break the loop.
CE100 Algorithms and Programming II
Graph Coloring
Naive Algorithm:
Create a recursive function that takes current index, number of vertices and output color array.
If the current index is equal to number of vertices.
Check if the output color configuration is safe,
i.e check if the adjacent vertices do not have same color.
If the conditions are met,
print the configuration and break.
Assign a color to a vertex (1 to m).
For every assigned color
recursively call the function with next index and number of vertices
If any recursive function returns true break the loop and returns true.
CE100 Algorithms and Programming II
Graph Coloring
Naive Complexity Analysis:
Time Complexity: O(mV).
There is a total O(mV) combination of colors. So the time complexity is O(mV).
Space Complexity: O(V).
Recursive Stack of graphColoring(…) function will require O(V) space.
CE100 Algorithms and Programming II
Graph Coloring
Backtracking Approach:
The idea is to assign colors one by one to different vertices,
starting from the vertex 0.
Before assigning a color, check for safety by considering already assigned colors to the adjacent vertices
i.e check if the adjacent vertices have the same color or not.
If there is any color assignment that does not violate the conditions,
mark the color assignment as part of the solution.
If no assignment of color is possible then backtrack and return false.
CE100 Algorithms and Programming II
Graph Coloring
Backtracking Algorithm:
Create a recursive function that takes the graph, current index, number of vertices, and output color array.
If the current index is equal to the number of vertices. Print the color configuration in output array.
Assign a color to a vertex (1 to m).
For every assigned color,
check if the configuration is safe,
(i.e. check if the adjacent vertices do not have the same color)
recursively call the function with next index and number of vertices
If any recursive function returns true break the loop and return true.
If no recursive function returns true then return false.
CE100 Algorithms and Programming II
Graph Coloring
Using BFS Approach / Algorithm
The approach here is to color each node from 1 to n
initially by color 1.
And start travelling BFS from an unvisited starting node to cover all connected components in one go.
On reaching each node during BFS traversal, do the following:
CE100 Algorithms and Programming II
Graph Coloring
Using BFS Approach / Algorithm
Check all edges of the given node.
For each vertex connected to our node via an edge:
check if the color of the nodes is the same.
If same,
increase the color of the other node (not the current) by one.
check if it visited or unvisited.
If not visited,
mark it as visited and push it in a queue.
Check condition for maxColors till now.
If it exceeds M, return false
After visiting all nodes,
return true (As no violating condition could be found while travelling).
CE100 Algorithms and Programming II
Graph Coloring
Using BFS Complexity Analysis:
Time Complexity: O(V+E).
Space Complexity: O(V).
For Storing Visited List.
CE100 Algorithms and Programming II
Biparitite Checker
CE100 Algorithms and Programming II
Biparitite Checker
A Bipartite Graph is a graph
whose vertices can be divided into
two independent sets,
U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U.
In other words, for every edge (u, v),
either u belongs to U and v to V,
or u belongs to V and v to U.
We can also say that there is no edge that connects vertices of same set.
CE100 Algorithms and Programming II
Biparitite Checker
A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.
CE100 Algorithms and Programming II
Biparitite Checker
CE100 Algorithms and Programming II
Biparitite Checker
Note that it is possible to color a cycle graph with even cycle using two colors.
CE100 Algorithms and Programming II
Biparitite Checker
It is not possible to color a cycle graph with odd cycle using two colors.
CE100 Algorithms and Programming II
Biparitite Checker Algorithm
One approach is to check whether the graph is 2-colorable or not using backtracking algorithm m coloring problem.
CE100 Algorithms and Programming II
Biparitite Checker Algorithm
Following is a simple algorithm to find out whether a given graph is Bipartite or not using
Breadth First Search (BFS).
CE100 Algorithms and Programming II
Biparitite Checker Algorithm
Assign RED color to the source vertex (putting into set U).
Color all the neighbors with BLUE color (putting into set V).
Color all neighbor’s neighbor with RED color (putting into set U).
This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)
CE100 Algorithms and Programming II
Disjoint Set Operations
CE100 Algorithms and Programming II
Disjoint Set Operations
A disjoint-set data structure
Maintains a collection S={s1,…,sk} of disjoint dynamic sets
Each set is identified by a representative which is some member of the set
In some applications,
It doesn't matter which member is used as the representative
We only care that,
if we ask for the representative of a set twice without
modifying the set between the requests,
we get the same answer both times
CE100 Algorithms and Programming II
Disjoint Set Operations
In other applications,
There may be a prescribed rule for choosing the representative
E.G. Choosing the smallest member in the set
Each element of a set is represented by an object "x"
MAKE-SET(x) creates a new set whose only member is x
Object x is the representative of the set
x is not already a member of any other set
UNION(x,y) unites the dynamic sets Sx&Sy that contain x&y
Sx&Sy are assumed to be disjoint prior to the operation
The new representative is some member of Sx∪Sy
CE100 Algorithms and Programming II
Disjoint Set Operations
Usually, the representative of either Sx or Sy is chosen as the new representative
We destroy sets Sx and Sy, removing them from the collection S since we require the sets in the collection to be disjoint
FIND-SET(x) returns a pointer to the representative of the unique set containing x
We will analyze the running times in terms of two parameters
n : The number of MAKE-SET operations
m : The total number of MAKE-SET, UNION and FIND-SET operations
CE100 Algorithms and Programming II
Disjoint Set Operations
Each union operation reduces the number of sets by one
since the sets are disjoint
Therefore, only one set remains after n−1 union operations
Thus, the number of union operations is $ \leq n – 1$
Also note that, m≥n always hold
since MAKE-SET operations are included in the total number of operations
CE100 Algorithms and Programming II
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
When we use both union-by-rank and path-compression the worst case running time is O(mα(m,n)) where α(m,n) is the very slowly growing inverse of the Ackerman’s function.
-In any conceivable application of disjoint-set data structure α(m,n)≤4.
Thus, we can view the running time as linear in practical situations.
CE100 Algorithms and Programming II
Minimum Spanning Tree (MST)
Kruskal
Prim
CE100 Algorithms and Programming II
Minimum Spanning Tree
One of the most famous greedy algorithms.
Weight is minimum over all
It has ∣V∣−1 edges
It has no cycles
It might not be unique
Undirected GraphG=(V,E)
Connected
Weight Function ω:E→R
Spanning Tree : Tree that connects all vertices
CE100 Algorithms and Programming II
Minimum Spanning Tree
MST : ω(T)=∑(u,v)∈Tω(u,v)
Note : MST is not unique.
CE100 Algorithms and Programming II
MST-Optimal Structure
Optimal Structure: Optimal tree has optimal subtrees.
Let T be an MST of G=(V,E)
Removing any edge (u,v) of T partitions T into two subtrees : T1&T2
Where T1=(V1,ET1)&T2=(V2,ET2)
CE100 Algorithms and Programming II
MST-Optimal Structure
Let G1=(V1,E1)&G2=(V2,E2) be
subgraphs induced by V1&V2
i.e. Ei={(x,y)∈E:x,y∈Vi}
Claim : T1&T2 are MSTs of G1&G2 respectively
Proof : ω(T)=ω(u,v)+ω(T1)+ω(T2)
There can’t be better trees than T1&T2 for G1&G2
Otherwise, T would be suboptimal for G
CE100 Algorithms and Programming II
Generic MST Algorithm
A is always a subset of some MST(s)
(u,v) is a safe edge for A if A∪{(u,v)} is also a subtree of some MST
---
## **Graph Traversal**
### Topological Sort
#### DFS version
```cpp
for each unvisited vertex u
DFS(u)
for each neighbor v of u
if v is unvisited, DFS(v)
else skip v;
finish DFS(u), add u to the back of list
reverse list
```