0% found this document useful (0 votes)
63 views15 pages

UNIT-4 (Graph)

Graph

Uploaded by

Dr. S.K. Sajan
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)
63 views15 pages

UNIT-4 (Graph)

Graph

Uploaded by

Dr. S.K. Sajan
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/ 15

UNIT – 4

Definition- Representation of Graph- Types of graph -Breadth first traversal – Depth first
traversal-Topological sort- Bi-connectivity – Cut vertex- Euler circuits-Applications of
graphs.

Graph: - A graph is data structure that consists of following two components.


 A finite set of vertices also called as nodes.
 A finite set of ordered pair of the form (u, v) called as edge.
(or)
A graph G= (V, E) is a collection of two sets V and E, where
V Finite number of vertices
E Finite number of Edges,

Edge is a pair (v, w), where v, w ∈ V.

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)}

DIFFERENCE BETWEEN TREES AND GRAPHS


Parameter Trees Graphs
Path Tree is special form of graph i.e. In graph there can be more than one path
minimally connected graph i.e. graph can have unidirectional or bi-
and having only one path directional paths (edges) between nodes
between any two vertices.
Loops Tree is a special case of Graph can have loops, circuits as
graph having no loops, no well as can have self-loops.
circuits and no self-loops.

In tree there is exactly one root In graph there is no such concept of


Root Node node and every child have only root node.
one parent.
In trees, there is parent child
Parent relationship so flow can be In Graph there is no such parent
Child there with direction top to child relationship.
relationship bottom or vice versa.

Trees are less complex then Graphs are more complex in


Complexity graphs as having no cycles, no compare to trees as it can have
self-loops and still connected. cycles, loops etc

Tree traversal is a kind of Graph is traversed by


Types of special case of traversal of DFS: Depth First Search
Traversal graph. Tree is traversed in Pre- BFS : Breadth First Search
Order, In-Order and Post-
algorithm
Order(all three in DFS or in
BFS algorithm)

In trees, there are many rules /


Connection restrictions for making In graphs no such rules/ restrictions
Rules connections between nodes are there for connecting the nodes
through edges. through edges.

DAG Trees come in the


category of DAG :
Directed Acyclic
Graphs is a kind of Graph can be Cyclic or Acyclic.
directed graph that have
no cycles.

Different types of trees are :


Different Binary Tree , Binary
Types Search Tree, AVL tree, There are mainly two types of
Heaps. Graphs :Directed and Undirected
graphs.
Tree applications: sorting and Graph applications : Coloring of maps,
Applications searching like Tree Traversal in OR (PERT & CPM), algorithms,
& Binary Search. Graph coloring, job scheduling, etc.

In Graph, no. of edges depends on


Tree always has n-1 edges. the graph.
No. of edges

Model Tree is a hierarchical model Graph is a network model


Other types of graphs:
Complete Graph:
A complete graph is a simple undirected graph in which every pair of distinct vertices is
connected by a unique edge.
OR
If an undirected graph of n vertices consists of n(n-1)/2 number of edges then the graph is
called complete graph.

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.

The two most common ways a Graph can be traversed are:


 Depth First Search (DFS)
 Breadth First Search (BFS)

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.

Depth First Search Traversal


Depth First Search is said to go "deep" because it visits a vertex, then an adjacent
vertex, and then that vertex' adjacent vertex, and so on, and in this way the distance from the
starting vertex increases for each recursive iteration.

How it works:

1. Start DFS traversal on a vertex.


2. Do a recursive DFS traversal on each of the adjacent vertices as long as they are not
already visited.

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.

Breadth First Search Traversal


Breadth First Search visits all adjacent vertices of a vertex before visiting
neighboring vertices to the adjacent vertices. This means that vertices with the same
distance from the starting vertex are visited before vertices further away from the starting
vertex are visited.

How it works:

1. Put the starting vertex into the queue.


2. For each vertex taken from the queue, visit the vertex, then put all unvisited adjacent
vertices into the queue.
3. Continue as long as there are vertices in the queue.

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

Interaction of biconnected components


 An edge belongs to exactly one biconnected component
 A nonseparation vertex belongs to exactly one biconnected component
 A separation vertex belongs to two or more biconnected components

Articulation Points (or Cut Vertices) in a Graph


The vertices whose removal disconnects the graph are known as Articulation Points.

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

Low(V) = min(Num(V), Num(W), Low(W))


 The Lowest Num(W) among all back edges V, W.
 The Lowest Low(W) among all the tree edges V, W.
 The root is an articulation if and only if (iff) it has more than two children.
 Any vertex V other than the root is an Articulation point iff V has some child W
such that Low(W)≥Num (V)
APPLICATION
 Bio-Connectivity is an application of depth first search.
 Used mainly in network concepts.
BICONNECTIVITY ADVANTAGES
 Total time to perform traversal is minimum.
 Adjacency lists are used
 Traversal is given by O(E+V).
DISADVANTAGES
 Have to be careful to avoid cycles
 Vertices should be carefully removed as it affects the rest of the graph.

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:

Conditions for Existence

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:

1. Starting from any vertex.


2. Following edges while avoiding bridges unless no other option exists.
3. Continuing until all edges have been traversed.

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 .

At each stage of the algorithm:


 the original graph minus the darkened (already used) edges = reduced graph
 important rule: never cross a bridge of the reduced graph unless there is no other
choice
Note:
 the same algorithm works for Euler paths
 before starting, use Euler’s theorems to check that the graph has an Euler path and/or
circuit to find.

APPLICATION
Eulerian paths are being used in bioinformatics to reconstruct the DNA SEQUENCE from
its fragments.

Applications of graph in data structure

Graphs are versatile data structures that model relationships and connections between
entities. Here are some key applications of graphs in data structures:

1. Social Networks: Graphs represent users as nodes and their connections


(friendships, follows) as edges. This allows for the analysis of social dynamics,
community detection, and recommendation systems.
2. Routing Algorithms: In networking, graphs model the connections between routers
and devices. Algorithms like Dijkstra's and A* find the shortest path between nodes,
optimizing data transmission routes.
3. Web Page Ranking: Search engines use graphs to represent web pages (nodes) and
hyperlinks (edges). Algorithms like PageRank evaluate the importance of pages
based on their connections.
4. Recommendation Systems: Graphs model relationships between users and items
(products, movies). Collaborative filtering uses graph traversal to suggest new items
based on user preferences.
5. Transportation Networks: Cities, roads, and routes can be represented as graphs,
enabling the analysis of traffic patterns, route planning, and optimization of logistics.
6. Dependency Resolution: In software development, graphs can represent
dependencies between modules or packages, helping in tasks like build automation
and version control.
7. Game Development: Graphs model game states and possible moves (like in chess
or checkers), facilitating the implementation of AI algorithms for decision-making.
8. Biological Networks: In bioinformatics, graphs model interactions between genes,
proteins, and other biological entities, aiding in understanding complex biological
systems.
9. Database Schema Representation: Graph databases (like Neo4j) use graphs to
model complex relationships in data, making it easier to perform queries that involve
multiple relationships.
10. Network Security: Graphs can model the relationships between devices in a
network, helping to identify vulnerabilities and attack paths.

Graphs are powerful tools in various fields due to their ability to represent complex
relationships and facilitate efficient algorithms for analysis and optimization.

You might also like