Graph_1
Graph_1
Introduction
• Generalization of a tree.
• Collection of vertices (or nodes) and connections
between them.
• No restriction on
– The number of vertices.
– The number of connections between the two
vertices.
• Have several real life applications.
Definition
• A graph G = (V,E) consists of a
– Non-empty set V of vertices and
– Possibly empty set E of edges.
• |V| denotes number of vertices.
• |E| denotes number of edges.
• An edge (or arc) is a pair of vertices (vi,vj) from V.
– Simple or undirected graph (vi,vj) = (vj,vi).
– Digraph or directed graph (vi,vj) ≠ (vj,vi).
• An edge has an associated weight or cost as well.
Undirected
Contd… Graph
v1 v4
v1 v4 Directed v2
Graph
v2 v5
v3 v6
v5
v3 v6
v1 v2
v1 v4
v2 v3
v4
v5 Complete
v3
Multigraph Graph
Representation – I
• Adjacency matrix
– Adjacency matrix for a graph G = (V, E) is a two
dimensional matrix of size |V| x |V| such that
each entry of this matrix
a[i][j] = 1 (or weight), if an edge (vi,vj) exists.
0, otherwise.
Non-Eulerian
graph as only
Eulerian graph as Euler path exists.
Euler circuit exists.
Fleury's algorithm
1. Start at a vertex of odd degree (Euler path), or, if
the graph has none, start with an arbitrarily
chosen vertex (Euler circuit).
2. Choose the next edge in the path to be one whose
deletion would not disconnect the graph. (Always
choose the non-bridge in between a bridge and a
non-bridge.)
3. Add that edge to the circuit, and delete it from the
graph.
4. Continue until the circuit is complete.
Example – 1
Vertex Degree
v1 2
v2 2
v3 4
v4 2
v5 2
v6 2
v6 e6 v3 e5 v5
v1 v2 v4 v2 v1v2
e3 or
e7 e2 e4
e1
v6 e6 v3 e5 v5
v1 v4 v3 v1v2v3
e3 or
e7 e4
e1e2
v6 e6 v3 e5 v5
Contd…
Graph Current Vertex Trail
v1 v4 v4 v1v2v3v4
v3v6 or e6 is a or
e7 e4
bridge and can’t e1e2e3
v6 e6 v3 e5 v5 be selected.
v1 v5 v1v2v3v4v5
or
e7
e1e2e3e4
v6 e6 v3 e5 v5
v1 v3 v1v2v3v4v5v3
or
e7
e1e2e3e4e5
v6 e6 v3
Contd…
Graph Current Vertex Trail
v1 v6 v1v2v3v4v5v3v6
or
e7
e1e2e3e4e5e6
v6
NULL v1 v1v2v3v4v5v3v6v1
or
e1e2e3e4e5e6e7
• Required Trail:
v1v2v3v4v5v3v6v1
or e1e2e3e4e5e6e7
Example – 2 Vertex Degree
v1 2
v2 2
v1 v2
v3 4
v4 3
v4 v5 2
v3 v5
v6 2
v6 v7 v8 v7 3
v8 2
v2 v4v2
v4v6 is a bridge
and can’t be
selected.
v1 v4v2v1
Contd…
Graph Current Vertex Trail
v3 v4v2v1v3
v4 v4v2v1v3v4
v6 v4v2v1v3v4v6
v7 v4v2v1v3v4v6v7
Contd…
Graph Current Vertex Trail
v5 v4v2v1v3v4v6v7v5
v8 v4v2v1v3v4v6v7v5v8
NULL v7 v4v2v1v3v4v6v7v5v8v7
v1 v2
• Required Trail:
v4 v5
v4v2v1v3v4v6v7v5v 8v 7 v3
v6 v7 v8
Example – 3
• Find Euler circuit using Fleury's algorithm. Start at
vertex A.
https://www.youtube.com/watch?v=vvP4Fg4r-Ns
Contd…
• In Fleury's algorithm, the graph traversal is linear in
the number of edges, i.e. O(|E|), excluding the
complexity of detecting bridges.
v6 v3 v5
v4 v1v2v3v4v5v3v6v1
v3 v5
Null Required Trail:
v1v2v3v4v5v3v6v1
Example – 2
• Find Euler circuit using Hierholzer’s algorithm. Start
at vertex A.
• Solution:
– ABCDEA
– AGFABCDEA
– AGFABEBCDEA
Contd…
• If doubly linked list is used to maintain
– The set of unused edges incident to each vertex,
– The list of vertices on the current tour that have unused
edges, and
– The tour itself.
• The individual operations of finding
– Unused edges exiting each vertex,
– A new starting vertex for a tour, and
– Connecting two tours that share a vertex.
• May be performed in constant time, so the
algorithm takes linear time, O(|E|).
Hamiltonian Graphs
• A path passing through all the vertices of a graph is
called a Hamiltonian path.
– A graph containing a Hamiltonian path is said to be
traceable.
• A cycle passing through all the vertices of a graph is
called a Hamiltonian cycle (or Hamiltonian circuit).
• A graph containing a Hamiltonian cycle is called a
Hamiltonian graph.
• Hamiltonian path problem: Determine the
existence of Hamiltonian paths and cycles in graphs
(NP-complete).
Example
• Not Hamiltonian graphs.
Example
•
• Hamiltonian graph
– 128765431
graph[1][4] = 1.
Thus Hamiltonian graph.
1 2 3 4
4 4 1 0 1 0 1
3 3 2 1 0 1 1
2 2 3 0 1 0 1
path[] 1 1 graph[][] 4 1 1 1 0
Applications
• Painting road lines,
• Plowing roads after a snowstorm,
• Checking meters along roads,
• Garbage pickup routes, etc.
Definitions
• Diagraph or directed graph.
• A cycle in a diagraph G is a set of edges, {(v1, v2), (v2, v3),
..., (vr −1, vr)} where v1 = vr .
• A diagraph is acyclic if it has no cycles. Also known as
directed acyclic graph (DAG).
• DAGs are used in many applications to indicate
precedence among events. For example,
– Inheritance between classes.
– Prerequisites between courses of a degree program.
– Scheduling constraints between the tasks of a projects.
Example – Prerequisites between courses
142 → 143 → 378 → 370 → 321 → 341 → 322 →
326 → 421 → 401
Example – Scheduling
Topological Sort
• Let, G = (V,E) be a DAG
• Linear ordering of all its
vertices such that if G contains
an edge (u,v), then u appears
before v in the ordering.
• Can also be viewed as an
ordering of vertices
along a horizontal line so
that all directed edges
go from left to right.
Kahn's algorithm
• L ← Empty list that will contain the sorted elements
• S ← Set of all nodes with no incoming edge
1. while S is non-empty do
2. remove a node n from S
3. add n to tail of L
4. for each node m with an edge e from n to m do
5. remove edge e from the graph
6. if m has no other incoming edges then
7. insert m into S
8. if graph has edges then
9. return error (graph has at least one cycle)
10. else
11. return L (a topologically sorted order)
Example – 1
B C
A F
D E
A B D Vertex In-degree
A 0
B C
B 1
C D E C 1
D E D 2
E 2
E
F 0
F
A B D
Contd… B C
L: Ø C D E
S: A → F
D E
L: A F
S: F → B
L: A → F
S: B
A B D
Contd… B C
L: A → F → B C D E
S: C
D E
F
L: A → F → B → C
S: D
Required
L: A → F → B → C → D
S: E topological
sort is
AFBCDE
L: A → F → B → C → D → E NULL
S: Ø
Topological Sort using DFS
• Perform DFS. Sort vertices in decreasing order of
their finishing time.
1 2 3 4
5 6 7 8
9 10 11 12
Contd…
Starting Finishing
Vertex
Time Time
A 1 10
B 6 9
C 7 8
D 2 5
E 3 4
F 11 12
0 1 2 3 4 5
5 0 1 1 1 1 1 1 0 0 0 0 0 0 0
4 0 0 0 0 0 0 1 1 0 0 0 0 0 0
3 0 0 0 0 1 1 1 2 0 0 0 1 0 0
2 0 0 0 1 1 1 1 3 0 1 0 0 0 0
1 0 0 0 0 0 1 1 4 1 1 0 0 0 0
• Visited[] 0 0 0 1 1 1 1 1 5 1 0 1 0 0 0
Example
• Topological sort:
0→1→2→3→4→5
Connected and Disconnected Graph
• A graph is connected if all the vertices are
connected to each other, i.e.
– A path exists between every pair of vertices.
– No unreachable vertices.
• {e1, e4}, {e6, e7}, {e1, e2, e3}, {e8}, {e3, e4, e5, e6}, {e2,
e5, e7}, {e2, e5, e6} and {e2, e3, e4}.
Cut
• A cut is a partition of the vertices of a graph into
two disjoint subsets.
• Formally,
In a graph G = (V,E), a pair of subsets V1 and V2 of V
satisfying
V = V1 ∪ V2, V1 ∩ V2 = ∅, V1 ≠ ∅, V2 ≠ ∅
is called a cut (or a partition) of G. Denoted as (V1, V2).
• It is also defined as an edge set.
cut (V1, V2) = {those edges with one end vertex in V1
and the other end vertex in V2}.
Contd…
• In an unweighted undirected Min-cut
graph, the size or weight of
a cut is the number of edges
crossing the cut.
b c
e f
Path-Based Depth-First Search Alg.
SCCs P
a
a
b c
e f
Path-Based Depth-First Search Alg.
SCCs P
a
a
b
b c
e f
Path-Based Depth-First Search Alg.
SCCs P
a
a
b
c
b c
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b
d
b
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b
d
b
e
d
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b
d
b
e
d
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e
f
b
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e
f
b
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e,f
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e,f
b
d
e f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e,f
Path-Based Depth-First Search Alg.
SCCs P
c a
a
b,d,e,f
a
Path-Based Depth-First Search Alg.
SCCs P
c
b,d,e,f
a
Complexity
𝑂(𝐸 + 𝑉)
Kosaraju-Sharir algorithm
Kosaraju-Sharir algorithm [2]
a
b c
0 - Finished.
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b c
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 2 - c
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 2 3 c
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 2 3 c
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 - 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
1 -12
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm
a
1 -12
𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
Kosaraju-Sharir algorithm [2]
a
b c
e f
Kosaraju-Sharir algorithm [2]
a
b c
e f
Kosaraju-Sharir algorithm [2]
a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
b c
e f
Kosaraju-Sharir algorithm [2]
SCCs a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
𝑎
b c
e f
Kosaraju-Sharir algorithm [2]
SCCs a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
𝑎
b c
e f
Kosaraju-Sharir algorithm [2]
SCCs a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
𝑎
𝑏, 𝑑, 𝑒, 𝑓
b c
e f
Kosaraju-Sharir algorithm [2]
SCCs a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
𝑎
𝑏, 𝑑, 𝑒, 𝑓
b c
e f
Kosaraju-Sharir algorithm [2]
SCCs a 𝑎, 𝑏, 𝑑, 𝑒, 𝑓, 𝑐
𝑎
𝑏, 𝑑, 𝑒, 𝑓
𝑐
b c
e f
Complexity
𝑂(𝐸 + 𝑉)