0% found this document useful (0 votes)
13 views26 pages

Unit Iv

The document provides detailed notes on Non-Linear Data Structures, specifically focusing on Graphs, including definitions, representations (Adjacency Matrix and List), types of graphs (Directed and Undirected), and various graph terminologies. It covers graph traversal methods such as Breadth-First Search (BFS) and Depth-First Search (DFS), along with algorithms for Topological Sort, Biconnectivity, and Euler Circuits. The notes are intended for B.E-CSE students at Panimalar Institute of Technology for the academic year 2018-19.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Unit Iv

The document provides detailed notes on Non-Linear Data Structures, specifically focusing on Graphs, including definitions, representations (Adjacency Matrix and List), types of graphs (Directed and Undirected), and various graph terminologies. It covers graph traversal methods such as Breadth-First Search (BFS) and Depth-First Search (DFS), along with algorithms for Topological Sort, Biconnectivity, and Euler Circuits. The notes are intended for B.E-CSE students at Panimalar Institute of Technology for the academic year 2018-19.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.

E-CSE

PANIMALAR INSTITUTE OF TECHNOLOGY

DEPARTMENT OF CSE

ACADEMIC YEAR :2018-19


BATCH :2017-2021
YEAR/SEM :II/III
SUB CODE : CS8391
SUB TITLE : DATA
STRUCTURS
UNIT-IV-NOTES
NON LINEAR DATA STRUCTURES - GRAPHS

CS8391 1 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

UNIT IV NON LINEAR DATA STRUCTURES - GRAPHS 9


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

DEFINITION
A Graph G = (V,E) consists of a set of vertices(V) and set of edges (E). Each
edge is a pair (v,w), where v,w € V. Edges are sometimes referred to as
arcs.
Example:

V = { V1,V2,V3,V4}
E = {E1, E2, E3, E4 }
REPRESENTATION OF GRAPHS
The two commonly used representations of Graphs are:
1. Adjacency Matrix
2. Adjacency List
1. Adjacency Matrix
Consider a graph C of n vertices and the matrix M. If there is an edge
present between vertices Vi and Vj, then M[i][j] = 1 else M[i][j] = 0. For an
undirected graph, M[i][j] = M[i][j = 1.
An Adjacency Matrix Representation for Undirected Graph

CS8391 2 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

An Adjacency Matrix Representation for Directed graph

Creating a Graph using Adjacency Matrix


Creation of graph using adjacency matrix is a simple task. The adjacency
matrix is nothing but a two dimensional array. The algorithm for creation of
graph using adjacency matrix is given below:

1) Declare an array of M [size][size] which stores the graph.


2) Enter the no. of nodes in the graph.
3) Enter the edges of the graph by two vertices each, say V i , Vj, which
indicates the edge.
4) If the graph is directed set M [i][j]= 1. If graph is undirected set M[i][j]
=M[i][j]=1.
5) When all the edges for the desired graph is entered print the graph M[i][j].

1. Adjacency List

CS8391 3 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

The type in which a graph is created with the linked list is called adjacency
list. So all the advantages of linked list can be obtained in this type of graph.
We need not have a prior knowledge of maximum number of nodes.

Construction of Adjacency List:


Since graph is a set of vertices and edges, we will maintain the two
structures, for vertices and edges respectively.
Consider the graph given below. It has vertices a,b,c,d & e.

The linked list representation of the head nodes and the adjacent nodes are
given below.
Struct head
{
char data;
struct head *down;
struct head *next;
}
struct node
{
int ver;
struct node *link
}

CS8391 4 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

TYPES OF GRAPHS
Graphs are classified into two types. They are:
i. Directed Graph.
ii. Undirected Graph.
i. Directed Graph: The directions are shown on the edges. Consider the
graph given below:
V1
E1
E2
V2 V3

E3 E4
V4

In the above graph, the edge E1 is in between the vertices V1 and V2. The
Vertex V1 is called the head and V2 is called the tail. It is also referred as
digraph.

ii. Undirected Graph: In this type of graph, the edges are not directed.

. In this type of graph the edge E is

CS8391 5 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Graph Terminologies:

Weighted Graph: A Graph is said to be weighted graph if every edge in the


graph is assigned a weight or value.

Unweighted Graph: A Graph is said to be unweighted graph if the edge in


the graph is not assigned a weight or value
Complete graph: If an undirected graph of n vertices consists of n(n-1)/2
number of edges then it is called as complete graph.
The graph shown below is a complete graph.

Sub graph: A Sub graph G’ of graph G is a graph such that the set of
vertices and
set of edges of G’ are proper subset of the set of edges of G.

CS8391 6 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Connected graph: An undirected graph is said to be connected if for every


pair of distinct vertices Vi and Vj in V(G) there is a path from Vi to Vj in G.

A directed graph with this property is called as strongly connected. If a


directed graph is not strongly connected, but the underlying graph is
connected, then the graph is said to be weekly connected.

Adjacency: Vertex w is adjacent to v if and only if (v, w)  E.

Length of a path: A path is a sequence of vertices w1, w2, w3, ..., wN, such
that (wi, wi+1)  E for 1 < i < N. The length of a path is the number of edges
on the path, which is N-1. A simple path is one such that all vertices are
distinct, except that the first and the last could be the same.
Cycle: A cycle in a directed graph is a path of length at least 1 such that w1
= wN.

Acyclic Graph: A directed graph is acyclic if it has no cycles (abbreviated


as DAG).

Indegree: The indegree of a vertex v is the number of edges entering into


the vertex v.

CS8391 7 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Outdegree: The outdegree of a vertex v is the number of edges leaving the


vertex v.

Example: Consider the graph given below:

The indegree of a is 0.
The outdegree of a is 2.

Graph Traversal:

There are two types of Graph Traversals. They are:


1. Breadth First Traversal also called as Breadth First Search (BFS)
2. Depth First Traversal also called as Depth First Search (DFS)

1. Breadth First Traversal also called as Breadth First Search (BFS)


In the BFS, a vertex V is taken visited first. Then the adjacent vertices of V
are visited. The same procedure is performed for all the vertices in the graph.

ALGORITHM
1. Create a graph.
2. Read the vertex from which you want to traverse the graph say Vi.
3. Initialise the visited array to 1 at the index of Vi

CS8391 8 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

4. Insert the visited vertex Vi in the queue


5. Visit the vertex which is at the front of the queue. Delete it from the
queue and place its adjacent nodes in the queue.
6. Repeat the step 5, till the queue is not empty
7. Stop.
Explanation of logic of BFS

In BFS the queue is maintained for storing the adjacent nodes and an array
“visited” is maintained for keeping the track of visited nodes. i.e. once a
particular t is visited it should not be revisited again.

CS8391 9 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

CS8391 10 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Increment front, delete the node from Queue and print it.

So output will be - BPS for above graph as


1 2 3 4
Routine:

2. Depth First Traversal also called as Depth First Search (DFS)


In this method, we follow the path as deeply as we can go. When there is no
adjacent vertex present we traverse back and search for unvisited vertex. We
will maintain a visited array to mark all the visited vertices. In case of DFS
the depth of a graph should be known.

CS8391 11 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

ALGORITHM
Step 1: Choose any node in the graph. Designate it as the search node and
mark it as visited.
Step 2: Using the adjacency matrix of the graph, find a node adjacent to the
search node that has not been visited yet. Designate this as the new search
node and mark it as visited.
Step 3: Repeat Step 2 using the new search node. If no node satisfying Step
2 can be found, return to the previous search node and continue.
Step 4: When a return to the previous node in step 3 is impossible, the
search from the originally chosen search node is complete.
Step 5: If the graph still contains unvisited nodes, choose any node that has
not been visited and repeat Step 1 through Step 4.

Example:

In DFS the basic data structure for storing the adjacent vertices is stack.

Step 1: Start with vertex 1, print it so ‘1’ gets printed. Mark I as visited.

CS8391 12 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

CS8391 13 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Routine for DFS:

Void DFS(Vertex V)
{
visisted[V]=True;
for each W adjacent to V
if(!visisted[W])
DFS(W);
}
TOPOLOGICAL SORT

DEFINITION

A topological sort is an ordering of vertices in a directed acyclic graph, such


that if there is a path from vi to vj, then vj appears after vi in the ordering.

Topological ordering is not possible, if the graph has a cycle, since for two
vertices v and w in a cycle, v precedes w and w precedes v.

Implementation of Topological Sort


Algorithm:
1. Find the indegree for every vertex.
2. Place the vertices whose indegree is ‘0’ on the empty queue.
3. Dequeue the vertex V and decrement the indegree’s of all its adjacent
vertices.
4. Enqueue the vertex on the queue, if its indegree falls to zero.
5. Repeat from step 3 until the queue becomes empty.

CS8391 14 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

6. The topological ordering is the order in which the vertices dequeued.

Consider the graph given below:

Step 1
Number of l’s present in each column of adjacency matrix represents the
indegree of corresponding vertex. In the given graph, indegree of a = 0,
b=1, c=2 and d=2.
Step 2
Enqueue the vertex, whose indegree is ‘0’
Since vertex ‘a’ is 0, so place it on the queue.
Step 3
Dequeue the vertex ‘a’ from the queue and decrement the indegree’s of its
adjacent vertex’
Step 4
Dequeue the vertex ‘b’ from Q and decrement the indegree’s of its adjacent
vertex.

CS8391 15 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Step 5
Dequeue the vertex c’ from Q and decrement the indegree’s of its adjacent
vertex..
Step 6
Dequeue the vertex ‘d’.

The topological sort for the graph is the vertices which are dequeued is
shown below.

The topological ordering is a b c d

Consider another example given below:

CS8391 16 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

The topological ordering is V1, V2, V5, V4, V3, V7, V6.

CS8391 17 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Routine to perform Topological Sort


void Topsort (Graph G)
{
Queue Q
mt counter = 0;
Vertex V, W;
Q = CreateQueue (NumVertex); Makeempty (Q); for each vertex V
if(indegree [ = = 0)
Enqueue (V, Q);
while (! IsEmpty (Q))
{
V = Dequeue (Q); TopNum [ = + + counter; for each W adjacent to V
if (--Indegree [ = = 0)
Enqueue (W, Q);
}
if (counter! = N% Vertex)
Error (“ Graph has a cycle”);
DisposeQueue (Q); 1 Free the Memory /
}
BICONNECTIVITY

A connected undirected graph is biconnected if there are no vertices


whose removal disconnects the rest of the graph.
If a graph is not biconnected, the vertices whose removal would
disconnect the graph are known as articulation points. These nodes are
critical in many applications. The graph given below is not biconnected. C

CS8391 18 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

and D are articulation points. The removal of C would disconnect G, and


the removal of D would disconnect E and F from the rest of the graph.

Consider the Graph given below:

CS8391 19 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Properties of Biconnected Graph:


1. There are two disjoint paths between any two vertices.
2. There exists simple cycle between two vertices.
3. There should not be any cut vertex. Cut vertex is a vertex which,
when removed from the graph, the graph becomes disconnected.

CS8391 20 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

EULER CIRCUITS
Definition:
An Eulerian path is a path in a graph which visits each edge exactly once.
Similarly, an Eulerian circuit is an Eulerian path which starts and ends on
the same vertex. They were first discussed by Leonhard Euler while solving
the famous Seven Bridges of Königsberg problem in 1736.

Mathematically the problem can be stated like this:

Given the graph on the right, is it possible to construct a path (or a cycle, i.e.
a path starting and ending on the same vertex) which visits each edge exactly
once?

Graphs which allow the construction of so called Eulerian circuits are called
Eulerian graphs. Euler observed that a necessary condition for the existence
of Eulerian circuits is that all vertices in the graph have an even degree, and
that for an Eulerian path either all, or all but two (i.e., the two endpoint)
vertices have an even degree.
Sometimes a graph that has an Eulerian path, but not an Eulerian circuit (in
other words, it is an open path, and does not start and end at the same vertex)
is called semi-Eulerian.

CS8391 21 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Graph 1 Graph 2

Graph 3 Graph 4

CS8391 22 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

Graph 5 Graph 6

What is the relationship between the nature of the vertices and the kind of
path/circuit that the graph contains?

Look at the table given below for the answer.

What does the


Graph Number of odd Number of path contain?
vertices even vertices (Euler path = P;
(vertices (vertices Euler circuit =
connected to an connected to an C;
odd number of even number of
edges) edges) Neither = N)

1 0 10 C

2 0 6 C

3 2 6 P

4 2 4 P

5 4 1 N

6 8 0 N

From the above table, we can observe that:

1. A graph with all vertices being even contains an Euler circuit.


CS8391 23 UNIT IV - DATA STRUCTURES
PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

2. A graph with 2 odd vertices and some even vertices contains an Euler
path.
3. A graph with more than 2 odd vertices does not contain any Euler
path or circuit.

Applications of Graph:

Graphs and Graph theory find various application in many real life
applications. Some of them are listed below:

1. The link structure of a website could be represented by a directed graph:


the vertices are the web pages available at the website and a directed edge
from page A to page B exists if and only if A contains a link to B.

2. A graph structure can be extended by assigning a weight to each edge of


the graph. Graphs with weights, or weighted graphs, are used to represent
structures in which pair wise connections have some numerical values. For
example if a graph represents a road network, the weights could represent
the length of each road. A digraph with weighted edges in the context of
graph theory is called a network.

3. Networks have many uses in the practical side of graph theory, network
analysis (for example, to model and analyze traffic networks). Within
network analysis, the definition of the term "network" varies, and may often
refer to a simple graph.

Many applications of graph theory exist in the form of network analysis.


These split broadly into three categories.

CS8391 24 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

i. Firstly, analysis to determine structural properties of a network, such as the


distribution of vertex degrees and the diameter of the graph. A vast number
of graph measures exist, and the production of useful ones for various
domains remains an active area of research.

ii. Secondly, analysis to find a measurable quantity within the network, for
example, for a transportation network, the level of vehicular flow within any
portion of it.

iii. Thirdly, analysis of dynamical properties of networks.

A directed graph may be used to represent a network of processing elements;


in this formulation, data enters a processing element through its incoming
edges and leaves the element through its outgoing edges. Examples of this
include the following:

 In electronic circuit design, a combinational logic circuit is an acyclic


system of logic gates that computes a function of an input, where the
input and output of the function are represented as individual bits.
 A Bayesian network represents a system of probabilistic events as
nodes in a directed acyclic graph. The likelihood of an event may be
calculated from the likelihoods of its predecessors in the DAG. In this
context, the moral graph of a DAG is the undirected graph created by
adding an (undirected) edge between all parents of the same node
(sometimes called marrying), and then replacing all directed edges by
undirected edges.
 Dataflow programming languages describe systems of values that are
related to each other by a directed acyclic graph. When one value

CS8391 25 UNIT IV - DATA STRUCTURES


PANIMALAR INSTITUTE OF TECHNOLOGY II YEAR/III SEM B.E-CSE

changes, its successors are recalculated; each value is evaluated as a


function of its predecessors in the DAG.

CS8391 26 UNIT IV - DATA STRUCTURES

You might also like