Day 5 application of graph ,biconnectivity fdp on ds
This document summarizes key concepts from a faculty development program on data structures, including graph applications, minimum spanning trees, shortest path algorithms, biconnected graphs, and Euler circuits. It provides examples and pseudocode for Prim's and Kruskal's minimum spanning tree algorithms and Dijkstra's shortest path algorithm. It also discusses identifying articulation points in a graph to determine if it is biconnected and conditions for the existence of Euler paths and circuits.
Day 5 application of graph ,biconnectivity fdp on ds
1.
VELAMMAL ENGINEERING COLLEGE
AnAutonomous Institution, Affiliated to Anna University
Chennai, & Approved by AICTE Delhi
Online Faculty Development Program on
Data Structures (CS8391)
From 25th – 29th May 2020
Organized by
Department of Computer Science and
Engineering
In Association with Computer Society of
India
APPLICATION OF GRAPH
•Graphs are widely used to model any situation
– where entities or things are related to each other in
pairs.
– For example, the following information can be
represented by graphs:
– Transportation networks
» In which nodes are airports, ports, etc.
» The edges can be airline flights, shipping
routes, etc.
– In circuit networks
» points of connection are drawn as vertices
» component wires become the edges of the
graph.
5/29/2020 Dept of CSE/VEC 4
5.
Application of Graphs
Googlemaps
– Intersection of two(or more) roads are considered to be a vertex
– Road connecting two vertices is considered to be an edge
– Navigation system is based on the algorithm to calculate the shortest path
between two vertices.
Facebook
– Users are considered to be the vertices
– If they are friends then there is an edge running between them.
– undirected graph.
World Wide Web
– web pages are considered to be the vertices.
– There is an edge from a page u to other page v if there is a link of page v on
page u
– Directed graph.
Operating System
– Resource Allocation Graph
– Each process and resources are considered to be vertices.
– Edges are drawn from resources to the allocated process
5/29/2020 Dept of CSE/VEC 5
6.
Minimum Spanning Trees
•A spanning tree of a connected, undirected graph G
– Sub-graph of G which is a tree that connects all the
vertices together.
• A graph G can have many different spanning trees.
• Minimum Spanning Tree
– Spanning tree of the smallest weight
– weight of a tree is defined as the sum of the weights on
all its edges.
• Minimum Spanning Tree problem
– Finding a minimum spanning tree for a given weighted
connected graph
5/29/2020 Dept of CSE/VEC 6
MST - Application
•MSTs are used to find airline routes.
– Vertices in the graph denote cities
– Edges represent the routes between these cities.
– MSTs are used to optimize airline routes by finding the
least costly path with no cycles.
• MSTs are also used to find the cheapest way to connect
terminals, such as cities, electronic components or
computers via roads, airlines, railways, wires or telephone
lines.
• MSTs are applied in routing algorithms for finding the most
efficient path.
5/29/2020 Dept of CSE/VEC 9
10.
Prim’s Algorithm
• Prim’salgorithm is a greedy algorithm
• Used to form a minimum spanning tree
for a connected weighted undirected
graph.
• Builds a tree that includes every vertex
and a subset of the edges in such a way
that the total weight of all the edges in
the tree is minimized.
• Tree vertices Vertices that are a part of
the minimum spanning tree T.
• Fringe vertices Vertices that are
currently not a part of T, but are adjacent
to some tree vertex.
• Unseen vertices Vertices that are
neither tree vertices nor fringe vertices
fall under this category.
5/29/2020 Dept of CSE/VEC 10
11.
ALGORITHM
//Input: A weightedconnected graph G = (V, E)
//Output: T , the set of edges composing a minimum
spanning tree of G
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe
vertices
Step 3: Select an edge e connecting the tree vertex
and fringe vertex that has minimum weight
Step 4: Add the selected edge and the vertex to the
minimum spanning tree T
Step 5: EXIT
5/29/2020 Dept of CSE/VEC 11
Example 2
Attach twolabels to a vertex
• the name of the nearest tree vertex,
• the weight of the corresponding edge.
select a as starting vertex
a(-, -)
b(a, 3)
c(-, ∞)
5/29/2020 Dept of CSE/VEC 14
Pseudo code
5/29/2020 Deptof CSE/VEC 19
T = ∅;
U = { 1 };
while (U ≠ V)
let (u, v) be the lowest cost edge such that
u ∈ U and v ∈ V - U;
T = T ∪ {(u, v)}
U = U ∪ {v}
20.
KRUSKAL ALGORITHM
Step-01:
Sort allthe edges from low weight to high weight.
Step-02:
Take the edge with the lowest weight and use it to connect the
vertices of graph.
If adding an edge creates a cycle, then reject that edge and go
for the next least weight edge.
Step-03:
Keep adding edges until all the vertices are connected and a
Minimum Spanning Tree (MST) is obtained.
5/29/2020 Dept of CSE/VEC 20
Prims Vs Kruskal
Kruskal’sAlgorithm is preferred when
– The graph is sparse.
– There are less number of edges in the graph
– The edges are already sorted or can be sorted in
linear time.
Prim’s Algorithm is preferred when
– The graph is dense.
– There are large number of edges in the graph
5/29/2020 Dept of CSE/VEC 27
28.
Time Complexity
Prims
• Ifa graph is represented by its weight matrix, then the
running time of Prim’s algorithm is , where n = |V|
• Let graph is represented by its adjacency lists
• Running time of Prim’s algorithm is in O(m log n),
– where m = |E|, n = |V|
Kruskal
O(ElogV)
5/29/2020 Dept of CSE/VEC 28
29.
DIJIKSTRA‘S ALGORITHM
• Single-sourceshortest-path problem
• For a given vertex called the source in a weighted
connected graph
– Find shortest paths to all its other vertices.
– The best-known algorithm for the single-source
shortest-paths problem is called Dijkstra’s
algorithm.
• First, it finds the shortest path from the source to a
vertex nearest to it, then to a second nearest, and so on
5/29/2020 Dept of CSE/VEC 29
30.
DIJIKSTRA ALGORITHM
contd..
Createa set Tree Vertices that keeps track of vertices included in
shortest path tree
Initially, this set is empty.
Assign a distance value to all vertices in the input graph.
Initialize all distance values as INFINITE.
Assign distance value as 0 for the source vertex so that it is
picked first.
While Tree Vertex doesn’t include all vertices
Pick a vertex u which is not there in Tree Vertex and has
minimum distance value.
Include u to Tree Vertex.
Update distance value of all adjacent vertices of u.
For every adjacent vertex v
if sum of distance value of u (from source) and weight of
edge u-v, is less than the distance value of v
then update the distance value of v.
5/29/2020 Dept of CSE/VEC 30
5/29/2020 Dept ofCSE/VEC 32
from a to b : a - b of length 3
from a to d : a - b - d of length 5
from a to c : a - b - c of length 7
from a to e : a - b - d - e of length 9
33.
• Dijikstra’s algorithmdoes not always work correctly
– If the edge weight is negative.
• The algorithm that is used to solve the negative
weighted, single-source shortest-paths path problem
– Bellman-Ford’s algorithm (using dynamic
programming).
• Time Analysis
– Adjacency Matrix Representation
• O(V2).
– Adjacency list
• O(ElogV)
5/29/2020 Dept of CSE/VEC 33
34.
Biconnected Graph
• Agraph with no articulation point
• If and only if any vertex is deleted, the graph remains
connected
5/29/2020 Dept of CSE/VEC 34
35.
5/29/2020 Dept ofCSE/VEC 35
First, starting at any vertex, we perform a depth-
first search and number the nodes as they are
visited.
For each vertex, v, call this preorder number
Num(v)
Low(v) is the minimum of
1. Num(v)
2. the lowest Num(w) among all back edges
(v, w)
3. the lowest Low(w) among all tree edges (v,
w)
Step 1: Find DFN
Step 2: Do Post order Traversal
and Find low value
Low(F)=min{Num(F),Num(D),-)
=min{6,4,-}=4
Low(E)=min{Num(E),-,Low(F)}
=min{5,-,4}=4
Low(D)=min{Num(D),Num(A),Low(E)}
=min{4,1,4}=1
36.
5/29/2020 Dept ofCSE/VEC 36
The root is an articulation point if and only
if it has more than one child
Any other vertex v is an articulation point if
and only if v has some child w such that
Low(w) ≥ Num(v)
Step 3: Rules to find Articulation Point
D has a child E, and Low(E) ≥ Num(D)
C and D are articulation points
C has a child G and Low(G) ≥ Num(C).
37.
Example
Step 1: FindDFN
Step 2: Do Post order Traversal and
Find low value
Ver
tex
0 1 2 3 4 5 6 7 8 9
dfn 4 3 2 0 1 5 6 7 9 8
low 4 0 0 0 0 5 5 5 9 8
5/29/2020 Dept of CSE/VEC 37
38.
the root, vertex3, is an
articulation point because it has
more than one child.
vertex 1 is an articulation point
since it has a child 0 such that
low (0) ≥ dfn (1)
Vertex 7 is also an articulation
point since low (8) ≥ dfn (7)
vertex 5 is also an articulation
point since low (6) ≥ dfn (5).
Ver
tex
0 1 2 3 4 5 6 7 8 9
dfn 4 3 2 0 1 5 6 7 9 8
low 4 0 0 0 0 5 5 5 9 8
5/29/2020 Dept of CSE/VEC 38
39.
Euler circuit
Eulerian pathand circuit for
undirected graph
• Eulerian Path is a path in graph
that visits every edge exactly
once.
• Eulerian Circuit is an Eulerian
Path which starts and ends on
the same vertex.
• An Euler path starts and ends
at different vertices.
• An Euler circuit starts and ends
at the same vertex
• A graph is called Eulerian if it
has an Eulerian Cycle and
called Semi-Eulerian if it has an
Eulerian Path
5/29/2020 Dept of CSE/VEC 39
40.
Euler Path Eg
5/29/2020Dept of CSE/VEC 40
Euler Path to exist in a graph, exactly 2 vertices must have odd degree
Start with one of the odd vertices. End in the other one
DFS to findEuler circuit
5/29/2020 Dept of CSE/VEC 43
2 , 1, 0, 2
2, 1,0, 3,4,0,2
Start with any vertex s.
First, using DFS find any circuit
starting and ending in s.
Mark all edges on the circuit as
visited
While there are still edges in the
graph that are not marked
visited:
• Find the first vertex v on
the circuit that has unvisited
edges.
• Find a circuit starting in v
and splice this path into the first
circuit
1,0,2,1
1,0,3,4,0,2,1
Solution 1
Solution 2