0% found this document useful (0 votes)
6 views117 pages

Graph_1

The document provides an overview of graphs, including definitions, types (such as undirected and directed), and representations (adjacency matrix and list). It discusses concepts like Eulerian paths and circuits, Fleury's and Hierholzer's algorithms for finding them, as well as Hamiltonian paths and cycles. Additionally, it covers methods for determining the existence of Hamiltonian paths using brute force and backtracking algorithms.

Uploaded by

Utkarsh Gourikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views117 pages

Graph_1

The document provides an overview of graphs, including definitions, types (such as undirected and directed), and representations (adjacency matrix and list). It discusses concepts like Eulerian paths and circuits, Fleury's and Hierholzer's algorithms for finding them, as well as Hamiltonian paths and cycles. Additionally, it covers methods for determining the existence of Hamiltonian paths using brute force and backtracking algorithms.

Uploaded by

Utkarsh Gourikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

Graphs

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.

– For an undirected graph, it is always a symmetric


matrix, as (vi, vj) = (vj, vi).
Adjacency matrix
Contd… (weighted)
Representation – II
• Adjacency list

– Uses an array of linked lists with size equals to |V|.

– An ith entry of an array points to a linked list of


vertices adjacent to vi.

– The weights of edges are stored in nodes of linked


lists to represent a weighted graph.
Adjacency List
• Undirected graph
Contd…
• Directed graph
Contd…
• Weighted directed graph
Pros and Cons
Adjacency Matrix Adjacency List
Memory Requirement O(|V|2) O(|V|+|E|)
Add Edge O(1) O(1)
Remove Edge O(1) O(|E|)
Find Edge Existence O(1) O(|V|)
Find # of Edges O(|V|2) O(|E|)
Add Vertex O(|V|2) O(|V|)
Remove Vertex O(|V|2) O(|E|)
Visit Adjacent Vertices O(|V|) O(Degree of that node)
Some Definitions
• Walk
– An alternating sequence of vertices and connecting
edges.
– Can end on the same vertex on which it began or on a
different vertex.
– Can travel over any edge and any vertex any number of
times.
• Path
– A walk that does not include any
vertex twice, except that its first
and last vertices might be the same.
Contd…
• Trail
– A walk that does not pass over the same edge twice.
– Might visit the same vertex twice, but only if it comes
and goes from a different edge each time.
• Cycle
– Path that begins and ends
on the same vertex.
• Circuit
–Trail that begins and ends
on the same vertex.
Euler Graphs
• An Eulerian trail (or Eulerian path) is a trail in a
graph which visits every edge exactly once.

• An Eulerian circuit (or Eulerian cycle) is an Eulerian


trail which starts and ends on the same vertex.

• A graph containing an Eulerian circuit is called Euler


graph.

• A connected graph G is an Euler graph if and only if


all vertices of G are of even degree.
Example

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

• As degree of each vertex is even, thus Euler circuit


exists.
Contd…
Graph Current Vertex Trail
v1 e1 v2 v4 v1 NULL
e3
e7 e2 e4

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

• As degree of all the vertices is even except two (v4


or v7), thus Euler path exists.
• Start either from v4 or v7.
Contd…
Graph Current Vertex Trail
v4 NULL

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.

• With Tarjan's linear time bridge-finding algorithm,


the time complexity is O(|E|2).

• With Thorup's dynamic bridge-finding algorithm,


the time complexity gets improved to
O(|E|(log|E|)3log log |E|).
Hierholzer's algorithm
1. Choose any starting vertex v, and follow a trail of
edges from that vertex until returning to v. The
tour formed in this way is a closed tour, but may
not cover all the vertices and edges of the initial
graph.
2. As long as there exists a vertex u that belongs to
the current tour but that has adjacent edges not
part of the tour, start another trail from u,
following unused edges until returning to u, and
join the tour formed in this way to the previous
tour.
Example – 1
Graph Trail
v1 v2 v4 v1v2v3v6v1

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

Not a Hamiltonian graph.


Solution 1 – Brute Force Search Algorithm
• A Hamiltonian Path in a graph having N vertices is
nothing but a permutation of the vertices of the
graph
[v1, v2, v3, ......vN-1, vN]
such that there is an edge between vi and vi+1 where
1 ≤ i ≤ N-1.
• So it can be checked for all permutations of the
vertices whether any of them represents a
Hamiltonian Path or not.
Contd…
function check_all_permutations(adj[][], n)
for i = 0 to n
p[i]=i
while next permutation is possible
valid = true
for i = 0 to n-1
if adj[p[i]][p[i+1]] == false
valid = false
break
if valid == true
print_permutation(p)
return true
p = get_next_permutation(p)
return false
Example
Not a Hamiltonian graph as
path exists and not a circuit.

1. 0-1-2-3 7. 1-0-2-3 13. 2-0-1-3 19. 3-0-1-2


2. 0-1-3-2 8. 1-0-3-2 14. 2-0-3-1 20. 3-0-2-1
3. 0-2-1-3 9. 1-2-0-3 15. 2-1-0-3 21. 3-1-0-2
4. 0-2-3-1 10. 1-2-3-0 16. 2-1-3-0 22. 3-1-2-0
5. 0-3-1-2 11. 1-3-0-2 17. 2-3-1-0 23. 3-2-0-1
6. 0-3-2-1 12. 1-3-2-0 18. 2-3-0-1 24. 3-2-1-0
Solution 2 – Backtracking
1. Create an empty path array and add vertex 0 to it.
2. Add other vertices, starting from the vertex 1.
3. Before adding a vertex, check for whether it is
adjacent to the previously added vertex and not
already added.
4. If such a vertex is found, add that vertex as part of
the solution.
5. Otherwise, return false.
Contd…
1. bool hamCycle(bool graph[V][V])
2. { int *path = new int[V];
3. for (int i = 0; i < V; i++)
4. path[i] = -1;
5.
6. path[0] = 0;
7. if ( hamCycleUtil(graph, path, 1) == false )
8. { printf("\nSolution does not exist");
9. return false;
10. }
11. printSolution(path);
12. return true; }
Contd…
1. bool hamCycleUtil(bool graph[V][V], int path[], int pos)
2. { if (pos == V)
3. { if ( graph[ path[pos-1] ][ path[0] ] == 1 )
4. return true;
5. else
6. return false;
7. }
8. for (int v = 1; v < V; v++)
9. { if (isSafe(v, graph, path, pos))
10. { path[pos] = v;
11. if (hamCycleUtil (graph, path, pos+1) == true)
12. return true;
13. path[pos] = -1;
14. }
15. }
16. return false; }
Contd…
1. bool isSafe(int v, bool graph[V][V], int path[], int pos)
2. {
3. if (graph [ path[pos-1] ][ v ] == 0)
4. return false;
5.
6. for (int i = 0; i < pos; i++)
7. if (path[i] == v)
8. return false;
9.
10. return true;
11. }
Example

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

• Required topological sort is F A B C D E.


Topological Sort using DFS – Implementation
1. T = []
2. visited = []
3. topological_sort( cur_vert, N, adj[][] )
4. { visited[cur_vert] = true
5. for i = 0 to N
6. if adj[cur_vert][i] is true and visited[i] is false
7. topological_sort(i, N, adj[][])
8. T.insert_in_beginning(cur_vert)
9. // T.push(cur_vert) }
Example
• T[]: 5 4 4→5→2→3
4 5
→1→0
3 2
2 3
1 1
0 0

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.

• A graph is disconnected if it is not connected.


• A graph with just one vertex is connected.
• An edgeless graph with two or more vertices is
disconnected.
Example
Cut Vertex
• A vertex v of a graph G is a cut vertex or an
articulation vertex of G if the graph G−v consists of
a greater number of components than G.
Contd...
• A graph is separable if it is not connected or if there
exists at least one cut vertex in the graph.
Cut-set
• A cut set of the connected graph G = (V, E) is an
edge set F ⊆E such that
1. G − F (remove the edges of F one by one) is not
connected, and
2. G − H is connected whenever H ⊂ F.
Example

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

• In a weighted graph, Max-cut


the value or weight is
defined by the sum of the
weights of the edges
crossing the cut.
Strongly Connected Components
A subset of a directed graph is called strongly connected
if there is a path in each direction between each pair of vertices of the subset.
Path-Based Depth-First Search Algorithm
Path-Based Depth-First Search Alg.
SCCs P

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

0 - Seen, not finished yet.

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

𝑂(𝐸 + 𝑉)

You might also like