0% found this document useful (0 votes)
1 views44 pages

8-Graph ADA

The document provides an overview of graphs as non-linear data structures consisting of vertices and edges, detailing various graph terminologies, properties, and representations. It covers elementary graph operations such as Depth First Search (DFS) and Breadth First Search (BFS), along with methods for graph traversal and topological sorting. Additionally, it explains the adjacency matrix and list representations, emphasizing their utility in graph manipulation and analysis.

Uploaded by

xaybzc1098765
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)
1 views44 pages

8-Graph ADA

The document provides an overview of graphs as non-linear data structures consisting of vertices and edges, detailing various graph terminologies, properties, and representations. It covers elementary graph operations such as Depth First Search (DFS) and Breadth First Search (BFS), along with methods for graph traversal and topological sorting. Additionally, it explains the adjacency matrix and list representations, emphasizing their utility in graph manipulation and analysis.

Uploaded by

xaybzc1098765
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/ 44

Exploring Graphs

Graphs
 What is Graph?
 Graph Terminologies
 Properties of Graph
 Representation of Graph
 Matrix representation of Graph
 Linked List representation of Graph
 Elementary Graph Operations
 Breadth First Search (BFS)
 Depth First Search (DFS)
 Topological Sort
 Source Removal Algorithm
 Bi-Connected Components
Non Linear Data Structures (Graph) 2
What is Graph ?
• A Graph is a non-linear data structure
consisting of vertices and edges. The
vertices are sometimes also referred to as
nodes and the edges are lines or arcs that
connect any two nodes in the graph. More
formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The
graph is denoted by G(E, V).

Non Linear Data Structures (Graph) 3


Components of Graphs
 Components of a Graph
• Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes.
Every node/vertex can be labeled or unlabelled.
• Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed
graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also
known as arcs. Every edge can be labeled/unlabelled.

Non Linear Data Structures (Graph) 4


Graph Terminologies
1. Undirected Graphs: A graph in which edges have no direction, i.e., the edges do not have arrows
indicating the direction of traversal. Example: A social network graph where friendships are not
directional.
2. Directed Graphs: A graph in which edges have a direction, i.e., the edges have arrows indicating the
direction of traversal. Example: A web page graph where links between pages are directional.
3. Weighted Graphs: A graph in which edges have weights or costs associated with them. Example: A
road network graph where the weights can represent the distance between two cities.
4. Unweighted Graphs: A graph in which edges have no weights or costs associated with them.
Example: A social network graph where the edges represent friendships.
5. Directed Acyclic Graph: A directed graph that contains no cycle is called as directed acyclic graph.

Non Linear Data Structures (Graph) 5


Graph Terminologies
1. Complete Graphs: A graph in which each vertex is connected to every other vertex. Example: A
tournament graph where every player plays against every other player.
2. Bipartite Graphs: A graph in which the vertices can be divided into two disjoint sets such that every
edge connects a vertex in one set to a vertex in the other set. Example: A job applicant graph where
the vertices can be divided into job applicants and job openings.
3. Trees: A connected graph with no cycles. Example: A family tree where each person is connected to
their parents.
4. Cycles: A graph with at least one cycle. Example: A bike-sharing graph where the cycles represent
the routes that the bikes take.

Non Linear Data Structures (Graph) 6


Graph Terminologies
1. Sparse Graphs: A graph with relatively few edges compared to the number of vertices. Example: A
chemical reaction graph where each vertex represents a chemical compound and each edge
represents a reaction between two compounds.
2. Dense Graphs: A graph with many edges compared to the number of vertices. Example: A social
network graph where each vertex represents a person and each edge represents a friendship.

Non Linear Data Structures (Graph) 7


Properties of Graph
1. Degree: The degree of a vertex is the number of edges that connect to it. In a directed graph, the in-
degree of a vertex is the number of edges that point to it, and the out-degree is the number of edges
that start from it.
2. Path: A path is a sequence of vertices that are connected by edges. A simple path does not contain
any repeated vertices or edges.
3. Cycle: A cycle is a path that starts and ends at the same vertex. A simple cycle does not contain any
repeated vertices or edges.
4. Connectedness: A graph is said to be connected if there is a path between any two vertices. A
disconnected graph is a graph that is not connected.
5. Bipartiteness: A graph is said to be bipartite if its vertices can be divided into two disjoint sets such
that no two vertices in the same set are connected by an edge.
6. Complete Graph: If an undirected graph with n vertices consists of n(n-1)/2 edges then it is called a
complete graph.

Non Linear Data Structures (Graph) 8


Adjacency matrix
 A diagrammatic representation of a graph may have limited usefulness. However such a
representation is not feasible when number of nodes an edges in a graph is large
 It is easy to store and manipulate matrices and hence the graphs represented by them in the
computer
 Let G = (V, E) be a simple diagraph in which V = {v1, v2,…., vn} and the nodes are assumed to be
ordered from v1 to vn
 An n x n matrix A is called Adjacency matrix of the graph G whose elements are aij are given by

1 𝑖𝑓 𝑉𝑖 , 𝑉𝑗 ∈ 𝐸
aij = ቊ
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Non Linear Data Structures (Graph) 9


Adjacency matrix
 An element of the adjacency matrix is either 0 or 1
 Any matrix whose elements are either 0 or 1 is called bit matrix or Boolean matrix
 For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the elements
of V
 For different ordering of the elements of V we get different adjacency matrices.

V1 V4 V1 V2 V3 V4
V1 0 1 0 1
A = V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
V2 V3

Non Linear Data Structures (Graph) 10


Adjacency matrix
V1 V4 V1 V2 V3 V4
V1 0 1 0 1
A= V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
V2 V3

 The number of elements in the ith row whose value is 1 is equal to the out-degree of node Vi
 The number of elements in the jth column whose value is 1 is equal to the in-degree of node Vj
 For a NULL graph which consist of only n nodes but no edges, the adjacency matrix has all its
elements 0. i.e. the adjacency matrix is the NULL matrix

Non Linear Data Structures (Graph) 11


Power of Adjacency matrix
1 1 0 1
0 1 0 1 1 1 0 0 1 1 0 0
1 0 0 0 0 1 0 1 A3 =
2 2 0 1
A= A2 = A x A =
1 1 0 1 1 2 0 1 0 1 0 1
0 1 0 0 1 0 0 0 1 2 0 1
1 1 0 1
A4 =
2 3 0 2
1 1 0 0

 Entry of 1 in ith row and jth column of A shows existence of an edge (Vi, Vj), that is a path of length 1
 Entry in A2 shows no of different paths of exactly length 2 from node Vi to Vj
 Entry in A3 shows no of different paths of exactly length 3 from node Vi to Vj

Non Linear Data Structures (Graph) 12


Path matrix or reachability matrix
 Let G = (V,E) be a simple diagraph which contains n nodes that are assumed to be ordered.
 A n x n matrix P is called path matrix whose elements are given by
1, 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑒𝑥𝑖𝑠𝑡𝑠 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑛𝑜𝑑𝑒 𝑉𝑖 𝑡𝑜 𝑉𝑗
𝑃𝑖𝑗 = ቊ
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Non Linear Data Structures (Graph) 13


Adjacency List Representation
0

4
1
2
3

0 1 2 3 4
1 0 3
2 0 3 4
3 0 1 2 4
4 0 2 3

Non Linear Data Structures (Graph) 14


Graph Traversal
 Two Commonly used Traversal Techniques are
 Depth First Search (DFS)
 Breadth First Search (BFS)

Non Linear Data Structures (Graph) 15


Depth First Search (DFS)
 It is like preorder traversal of tree
 Traversal can start from any vertex Vi
 Vi is visited and then all vertices adjacent to Vi are traversed recursively using DFS
DFS (G, 1) is given by

1 Step 1: Visit (1)
Step 2: DFS (G, 2) DFS (G, 2):
✓ 2 DFS (G, 3) Step1: Visit(2)
✓ 5 DFS (G, 4) Step 2: DFS (G, 6)
✓ 3 ✓ 4 DFS (G, 5) DFS (G, 6):
Step1: Visit(6)
Step 2: DFS (G, 3)
DFS (G, 8)
✓ 6 ✓ 7
✓ DFS of given graph starting from node 1 is given by

8 1 2 6 3 8 7 4 5
Non Linear Data Structures (Graph) 16
Depth First Search (DFS)

✓ A

✓ B C ✓
M N O
✓ D ✓ E F ✓ G ✓
R
Q P
H

ABDHECFG
✓ A

✓ B ✓ C E A B D C F E
✓ D F✓
Non Linear Data Structures (Graph) 17
Breadth First Search (BFS)
 This methods starts from vertex V0
 V0 is marked as visited. All vertices adjacent to V0 are visited next
 Let vertices adjacent to V0 are V1, V2, V2, V4
 V1, V2, V3 and V4 are marked visited
 All unvisited vertices adjacent to V1, V2, V3, V4 are visited next
 The method continuous until all vertices are visited
 The algorithm for BFS has to maintain a list of vertices which have been visited but not
explored for adjacent vertices
 The vertices which have been visited but not explored for adjacent vertices can be stored in
queue

Non Linear Data Structures (Graph) 18


Breadth First Search (BFS)
✓ 1 ✓ A
✓ ✓
2 5 ✓ B ✓ C
✓ ✓

3 4 ✓ D ✓ E F✓ G

6 ✓ 7 ✓
H✓
✓ 8 1 |2 3 4 5| 6 7| 8 A|BC |DEFG |H
V4
V1
V0| V1 V2 | V4 V6 V3 | V5
V2
V0 V6

V3
V5

Non Linear Data Structures (Graph) 19


Write DFS & BFS of following Graphs

A
B C
M N O
D E F G
R
Q P
H

A A
0 1
B C E B E
5 2
D F
4 3
C D
Non Linear Data Structures (Graph) 20
Procedure : DFS (vertex V)
 This procedure traverse the graph G in DFS manner.
 V is a starting vertex to be explored.
 Visited[] is an array which tells you whether particular vertex is visited or not.
 W is a adjacent node of vertex V.
 S is a Stack, PUSH and POP are functions to insert and remove from stack respectively.

Non Linear Data Structures (Graph) 21


Procedure : DFS (vertex V)
1. [Initialize TOP and Visited]
visited[]  0
TOP  0
2. [Push vertex into stack]
PUSH (V)
3. [Repeat while stack is not Empty]
Repeat Step 3 while stack is not empty
v  POP()
if visited[v] is 0
then visited [v]  1
for all W adjacent to v
if visited [w] is 0
then PUSH (W)
end for
end if
Non Linear Data Structures (Graph) 22
Procedure : BFS (vertex V)
 This procedure traverse the graph G in BFS manner
 V is a starting vertex to be explored
 Q is a queue
 visited[] is an array which tells you whether particular vertex is visited or not
 W is a adjacent node f vertex V.

Non Linear Data Structures (Graph) 23


Procedure : BFS (vertex V)
1. [Initialize Queue & Visited]
visited[]  0
F  R  0
2. [Marks visited of V as 1]
visited[v]  1
3. [Add vertex v to Q]
InsertQueue(V)
4. [Repeat while Q is not Empty]
Repeat while Q is not empty
v  RemoveFromQueue()
For all vertices W adjacent to v
If visited[w] is 0
Then visited[w]  1
InsertQueue(w)

Non Linear Data Structures (Graph) 24


Topological Sorting
 Topological Sorting or Kahn's algorithm is an algorithm that orders a directed acyclic graph in a way such that
each node appears before all the nodes it points to in the returned order, i.e. if we have a --> b, a must appear
before b in the topological order.
 Its main usage is to detect cycles in directed graphs since no topological order is possible for a graph that
contains a cycle. Some of its uses are deadlock detection in OS, Course schedule problems, etc.
 Essentially, topological sort is an algorithm which sorts a directed graph by returning an array or a vector, or a list,
that consists of nodes where each node appears before all the nodes it points to.

Non Linear Data Structures (Graph) 25


DFS-based Topological Sorting Algorithm
 We can modify DFS to find the Topological Sorting of a graph. In DFS,
 We start from a vertex, we first print it, and then
 Recursively call DFS for its adjacent vertices.

 In topological sorting,
 We use a temporary stack.

 We don’t print the vertex immediately,

 we first recursively call topological sorting for all its adjacent vertices, then push it to a stack.

 Finally, print the contents of the stack.

 Note: A vertex is pushed to stack only when all of its adjacent vertices (and their adjacent vertices and so on) are
already in the stack

Non Linear Data Structures (Graph) 26


 Approach:
• Create a stack to store the nodes.
• Initialize visited array of size N to keep the record of visited nodes.
• Run a loop from 0 till N
• if the node is not marked True in visited array
• Call the recursive function for topological sort and perform the following steps.
• Mark the current node as True in the visited array.
• Run a loop on all the nodes which has a directed edge to the current node
• if the node is not marked True in the visited array:
• Recursively call the topological sort function on the node
• Push the current node in the stack.
• Print all the elements in the stack.

Non Linear Data Structures (Graph) 27


Non Linear Data Structures (Graph) 28
Source Removal Algorithm
 Kahn's algorithm basically looks for the nodes that do not have any incoming edges, or have indegree = 0, and then
removes their outgoing edges, making its outdegree also equal to 0.
 Here's the algorithm step by step:
1. Find a vertex that has indegree = 0 (no incoming edges)
2. Remove all the edges from that vertex that go outward (make its outdegree = 0, remove outgoing edges)
3. Add that vertex to the array representing the topological sorting of the graph
4. Repeat till there are no more vertices left.

Non Linear Data Structures (Graph) 29


Non Linear Data Structures (Graph) 30
Bi-Connected Components
 A graph is a Biconnected graph if:
 There is a path from any node to any other node, i.e. the graph must be connected.
 After removing any node and all the associated edges from the graph, it still remains connected, i.e. there is
always a path between any two nodes even after removing any node from the graph.
 A graph is a Biconnected graph if it contains no articulation points.

Non Linear Data Structures (Graph) 31


Articulation Point
 Articulation points represent vulnerabilities in a connected network – single points whose
failure would split the network into 2 or more components. They are useful for designing
reliable networks.

Non Linear Data Structures (Graph) 32


Naive approach to find Articulation Points in a Graph:

 A simple approach is to one by one remove all vertices and see if removal of a vertex causes
disconnected graph.
 This method is the easiest approach. The basic idea is to iteratively remove a node at a time from the graph
and see which nodes cause the graph to be compartmentalized.
 Even though this method is effective at finding articulation point, as it should be, it has the high time
complexity as the tree has to be traversed each time per node.
 The time complexity = O(v*(v+e))

 Following the below steps to Implement the idea:


 Iterate over all the vertices and for every vertex do the following:
 Remove v from graph
 See if the graph remains connected (We can either use BFS or DFS)
 Add v back to the graph

Non Linear Data Structures (Graph) 33


Types of Edges in DFS
• Tree Edge: It is an edge which is present in the tree obtained
after applying DFS on the graph. All the Green edges are tree
edges.
• Forward Edge: It is an edge (u, v) such that v is a descendant
but not part of the DFS tree. An edge from 1 to 8 is a forward
edge.
• Back edge: It is an edge (u, v) such that v is the ancestor of
node u but is not part of the DFS tree. Edge from 6 to 2 is a
back edge. Presence of back edge indicates a cycle in
directed graph.
• Cross Edge: It is an edge that connects two nodes such that
they do not have any ancestor and a descendant relationship
between them. The edge from node 5 to 4 is a cross edge.

Non Linear Data Structures (Graph) 34


Basic Terminologies for Tarjan’s Algorithm
 Discovery Time(disc[node]): It is the time when a particular node is visited in a DFS call. It
always starts with 1 (for the sake of implementation, since all we care about is which node is
discovered when) and is incremented by 1 as we visit a new node in the DFS call.

Non Linear Data Structures (Graph) 35


 Parent Array(par[node]): We will also maintain a parent array to keep track of the parent of each
node. Parent[i] for a node i determines from which node did the DFS call has been made to
node i. Source node has parent = -1.

Non Linear Data Structures (Graph) 36


• Lowest Reachable Ancestor(low[node]): The low value of a node is the
lowest possible discovery time of a previously discovered node if we ignore
the current DFS path.

• Low[1] = 1, because it is the first node to be discovered in DFS call


• Low[2] = 2, because there is no other path leading to any other
previously discovered node except the current path.
• Low[3] = 3, because there is no other path leading to any other
previously discovered node except the current path.
• Low[4] = 1, because there is a path from node 4 to node 1, which is
not the current DFS path and is leading to a previously discovered
node.
• Low[5] = 5, because there is no other path leading to any other
previously discovered node except the current path.

Non Linear Data Structures (Graph) 37


Finding Articulation Points in a Graph using Tarjan’s Algorithm:

 The idea is to use DFS (Depth First Search). In DFS, follow vertices in a tree form called the DFS
tree. In the DFS tree, a vertex u is the parent of another vertex v, if v is discovered by u.

 In DFS tree, a vertex u is an articulation point if one of the following two conditions is true.

 u is the root of the DFS tree and it has at least two children.
 u is not the root of the DFS tree and it has a child v such that no vertex in the subtree rooted with v has a
back edge to one of the ancestors in DFS tree of u.

Non Linear Data Structures (Graph) 38


How to Detect AP for Case 1
 Keep Children counter for each node.
 On calling DFS on each edge, mark the entire subgraph.
 Call DFS on other edges only if node is not visited.
 Increment children counter on each unique DFS call.
 If children > 1 then the node is AP else not.

Non Linear Data Structures (Graph) 39


How to detect AP for case 2
 Case 2: Node u is not root node of DFS tree and it has a child v such that no vertex in subtree
rooted with v has a back edge to one of the ancestor of u.
 We need to find the order of vertices from earliest to latest to detect back edges.
 Therefore, we use time stamp to mark nodes with increasing value.
 Therefore, we can do this by assigning discovery time.
 We need to maintain earliest possible node accessible for a given node which will indicate if we have any
back edge. And for that we will assign low value to each node.
 Edge from child to parent is not a back edge.
 We do not care about edge to parent because parent is going to get removed for AP check.
 Child should point to an ancestor and not the parent to qualify to become a back edge.

Non Linear Data Structures (Graph) 40


Tarjan’s Algorithm to Find Articulation Points
 One efficient algorithm to identify articulation points in an undirected graph is Tarjan's Algorithm. This algorithm
is based on Depth First Search (DFS) traversal and has a time complexity of O(V + E), where V is the number of
vertices and E is the number of edges in the graph.

 The algorithm uses three arrays:


 visited: To keep track of visited nodes during DFS traversal.
 discovery_time: To store the discovery time (DFS start time) of each node.
 low: To store the lowest discovery time reachable from a node through its subtree.
 The algorithm also uses a time variable to keep track of the current discovery time during the DFS traversal.

Non Linear Data Structures (Graph) 41


 Initialize the visited, discovery_time, and low arrays to have a length equal to the number of
nodes in the graph, and set all their values to -1.
 Set the time variable to 0.
 Perform a DFS traversal on the graph, starting from an arbitrary node (e.g., node 0). During the
traversal, for each node u:
 a. Mark u as visited and set its discovery_time[u] and low[u] to the current value of time.
 b. Increment the time variable.
 c. For each neighbor v of u:

i. If `v` is not visited, set `v` as the parent of `u` and perform a DFS from `v`.

ii. After returning from the DFS call for `v`, update the `low[u]` value as the minimum of `low[u]` and `low[v]`.
iii. If `u` is not the root node and `low[v] >= discovery_time[u]`, then `u` is an articulation point.

iv. If `u` is the root node and has at least two children in the DFS tree, then `u` is an articulation point.

Non Linear Data Structures (Graph) 42


Identification of Bi-Connected Components
 For a given graph, a Biconnected Component is one of its subgraphs that is Biconnected. This
means there is always a path between any two nodes in the component, even after removing
any node from the component.
 Some key observation can be made in connection with the bi-connected components of the
graph:
 Two different bi-connected components should not have any common edges.
 Two different bi-connected components can have a common vertex.
 The common vertex which is attaching two or more bi-connected must be the articulation point of the given
graph.

Non Linear Data Structures (Graph) 43


Thank
You

You might also like