UNIT-4 (Graph)
UNIT-4 (Graph)
Definition- Representation of Graph- Types of graph -Breadth first traversal – Depth first
traversal-Topological sort- Bi-connectivity – Cut vertex- Euler circuits-Applications of
graphs.
Types of Graphs:
Directed graph
Undirected Graph
Directed Graph:
In representing of graph there is a directions are shown on the edges then that graph is
called Directed graph.
That is,
A graph G= (V, E) is a directed graph, Edge is a pair (v, w), where v, w ∈ V, and the pair is
ordered. Means vertex ‘w’ is adjacent to v. Directed graph is also called digraph.
Undirected Graph:
In graph vertices are not ordered is called undirected graph. Means in which (graph)
there is no direction (arrow head) on any line (edge).
A graph G= (V, E) is a directed graph, Edge is a pair (v, w), where v, w ∈ V, and the pair
is not ordered. Means vertex ‘w’ is adjacent to ‘v’, and vertex ‘v’ is adjacent to ‘w’.
Note: in graph there is another component called weight/ cost.
Weight graph:
Edge may be weight to show that there is a cost to go from one vertex to another.
Example: In graph of roads (edges) that connect one city to another (vertices), the weight
on the edge might represent the distance between the two cities (vertices).
V= {0, 1, 2, 3, 4}
E={(0,1,3), (0,3,7), (0,4,8), (1,2,1), (1,3,4), (2,3,2), (3,4,3)}
Connected Graph:
A graph which is connected in the sense of a topological space (study of shapes), i.e., there
is a path from any point to any other point in the graph. A graph that is not connected is said
to be disconnected.
Degree:
The degree of a graph vertex v of a graph G is the number of graph edges which touch v.
The vertex degree is also called the local degree or valency. Or
The degree (or valence) of a vertex is the number of edge ends at that vertex.
For example, in this graph all of the vertices have degree three.
In a digraph (directed graph) the degree is usually divided into the in-degree and the
outdegree
In-degree:
The in-degree of a vertex v is the number of edges with v as their terminal vertex.
Out-degree:
The out-degree of a vertex v is the number of edges with v as their initial vertex.
TOPOLOGICAL SORT
A topological sort is a linear ordering of vertices in a Directed Acyclic Graph such that if
there is a path from Vi to Vp, then Vj appears after Vi in the linear ordering.Topological
sort is not possible if the graph has a cycle.
INTRODUCTION
In graph theory, a topological sort or topological ordering of a directed acyclic graph
(DAG) is a linear ordering of its nodes in which each node comes before all nodes to
which it has outbound edges.
Every DAG has one or more topological sorts.
More formally, define the partial order relation R over the nodes of the DAG such
that xRy if and only if there is a directed path from x to y.
Then, a topological sort is a linear extension of this partial order, that is, a total order
compatible with the partial order.
PROCEDURE
Step – 1 : Find the indegree for every vertex.
Step – 2 : Place the vertice whose indegree is 0, on the empty queue.
Step – 3 : Dequeue the vertex V and decrement the indegrees of all its adjacent vertices.
Step – 4 : Enqueue the vertex on the queue if its indegree falls to zero.
Step – 5 : Repeat from Step -3 until the queue becomes empty.
The topological ordering is the order in which the vertices dequeue.
Vertices Indegree
1 0
2 0
Graphs Traversal
To traverse a Graph means to start in one vertex, and go along the edges to visit other
vertices until all vertices, or as many as possible, have been visited.
DFS is usually implemented using a Stack or by the use of recursion (which utilizes the call
stack), while BFS is usually implemented using a Queue.
How it works:
The DFS traversal starts in vertex D, marks vertex D as visited. Then, for every new vertex
visited, the traversal method is called recursively on all adjacent vertices that have not been
visited yet. So when vertex A is visited in the animation above, vertex C or vertex E
(depending on the implementation) is the next vertex where the traversal continues.
How it works:
As you can see in the animation above, BFS traversal visits vertices the same distance from
the starting vertex, before visiting vertices further away. So for example, after visiting
vertex A, vertex E and C are visited before visiting B, F and G because those vertices are
further away.
Breadth First Search traversal works this way by putting all adjacent vertices in a queue (if
they are not already visited), and then using the queue to visit the next vertex.
BICONNECTIVITY :
A connected undirected graph is biconnective if there are no vertices whose removal
disconnects the rest of the graph.
A biconnected undirected graph is a connected graph that is not broken into
disconnected pieces by deleting any single vertex (and its incident edges).
A biconnected directed graph is one such that for any two vertices v and w there are
two directed paths from v to w which have no vertices in common other than v and
w.
If a graph is not bi-connected, the vertices whose removal would disconnect the
graph is called articulation points.
Biconnected Components
Biconnected component of a graph G are:
A maximal biconnected subgraph of G, or
A subgraph consisting of a separation edge of G and its end vertices
.
Steps to find Articulation Points:
(i) Perform DFS, starting at any vertex.
(ii) Number the vertex as they are visited as Num(V).
(iii) Compute the lowest numbered vertex for every vertex V in the DFS tree, which we call
as low(W), that is reachable from V by taking one or more tree edges and then possible
one back edge by definition.
An Euler circuit (or Eulerian circuit) is a path in a graph that visits every edge exactly once
and returns to the starting vertex. Here are some key points about Euler circuits:
1. Connected Graph: The graph must be connected, meaning there is a path between
any pair of vertices.
2. Even Degree: Every vertex in the graph must have an even degree (an even number
of edges incident to it).
Applications
Route Planning: Euler circuits can be used to design efficient routes for tasks like
garbage collection, mail delivery, and snow plowing.
Network Design: They help in optimizing the layout of networks and circuits in
engineering.
DNA Sequencing: In bioinformatics, Eulerian paths are used in reconstructing
sequences from fragments.
Example
A classic example of an Euler circuit is the Seven Bridges of Königsberg problem. The
challenge was to find a walk through the city that would cross each of its seven bridges
exactly once. Euler proved that such a walk was impossible because the conditions for an
Euler circuit were not met.
Finding an Euler Circuit
If a graph meets the criteria for an Euler circuit, one common algorithm to find it is Fleury’s
algorithm, which involves:
Euler circuits are a fascinating concept in graph theory and have various practical
applications in real-world problems!
EULER CIRCUIT
EULERIAN PATH
An Eulerian path in an undirected graph is a path that uses each edge exactly once. If such a
path exists, the graph is called traversable or semi-eulerian.
EULERIAN CIRCUIT
An Eulerian circuit or Euler tour in an undirected graph is a cycle that uses each edge
exactly once. If such a cycle exists, the graph is called Unicursal. While such graphs are
Eulerian graphs, not every Eulerian graph possesses an Eulerian cycle.
EULER'S THEOREM
Euler's theorem 1
If a graph has any vertex of odd degree then it cannot have an Euler
circuit.
If a graph is connected and every vertex is of even degree, then it at
least has one Euler circuit.
Euler's theorem 2
If a graph has more than two vertices of odd degree then it cannot
have an Euler path.
If a graph is connected and has just two vertices of odd degree, then
it at least has one Euler path. Any such path must start at one of the
odd vertices and end at the other odd vertex.
ALGORITHM
Fleury's Algorithm for finding an Euler Circuit
1. Check to make sure that the graph is connected and all vertices are of even degree
2. Start at any vertex
3. Travel through an edge:
If it is not a bridge for the untraveled part, or
there is no other alternative
4. Label the edges in the order in which you travel them.
5. When you cannot travel any more, stop
Fleury's Algorithm
1. pick any vertex to start .
2. from that vertex pick an edge to traverse .
3. darken that edge, as a reminder that you can't traverse it again .
4. travel that edge, coming to the next vertex .
5. repeat 2-4 until all edges have been traversed, and you are back at the starting vertex .
APPLICATION
Eulerian paths are being used in bioinformatics to reconstruct the DNA SEQUENCE from
its fragments.
Graphs are versatile data structures that model relationships and connections between
entities. Here are some key applications of graphs in data structures:
Graphs are powerful tools in various fields due to their ability to represent complex
relationships and facilitate efficient algorithms for analysis and optimization.