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

Unit 3 Graph

The document provides an overview of graph theory, defining graphs as collections of vertices and edges, and distinguishing between directed and undirected graphs. It covers various graph terminologies, types of graphs, and their applications in fields such as electrical engineering and computer science. Additionally, it explains graph representation methods and traversal techniques, including breadth-first search (BFS) and depth-first search (DFS).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views44 pages

Unit 3 Graph

The document provides an overview of graph theory, defining graphs as collections of vertices and edges, and distinguishing between directed and undirected graphs. It covers various graph terminologies, types of graphs, and their applications in fields such as electrical engineering and computer science. Additionally, it explains graph representation methods and traversal techniques, including breadth-first search (BFS) and depth-first search (DFS).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Unit 3- Graph

Grap
h
• A graph can be defined as group of vertices and edges that are used to connect these
vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any
complex relationship among them instead of having parent child relationship.
• A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are used to connect these vertices.
• A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E),
(E,D), (D,B), (D,A)) is shown in the following figure.
Directed and Undirected Graph
• A graph can be directed or undirected. However, in an undirected graph, edges are
not associated with the directions with them. An undirected graph is shown in the
above figure since its edges are not attached with any of the directions. If an edge
exists between vertex A and B then the vertices can be traversed from B to A as well
as A to B.
• In a directed graph, edges form an ordered pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is called initial node while node B is
called terminal node.
• A directed graph is shown in the following figure.
Graph Terminology
•Degree of vertex
The degree of a vertex is the number of edges
connecting it. Vertex Degree
Notation – degree (V) a 2
b 3
Degree of Vertex in a Directed Graph c 1
d 2
e 0
Degree of Vertex in a Directed Graph
In a directed graph, each vertex has an in-degree and an out-degree.
In-degree of vertex V is the number of edges that are coming into the
vertex V and Out-degree of vertex V is the number of edges that are
going out from the vertex V.

In Degree Notation: deg−(V).


Out Degree deg+(V).
Notation:
Consider the following example.

Vertex In-degree Out-degree


a 1 2
b 2 0
c 2 1
d 1 1
e 1 1
f 1 1
g 0 2
Adjacent Vertices
Two nodes or vertices are adjacent if they are connected to each other through an edge. Vertex v1 is adjacent to a
vertex v2 if there is an edge (v1, v2) or (v2, v1). Let us consider the following Graph:

Vertices adjacent to node 2: 1 and 4


Vertices adjacent to node 4: 2, 3 and 6
Path
A path is a sequence of vertices with the property that each vertex in the
sequence is adjacent to the vertex next to it.
Example: Undirected Graph
Possible Path from vertex a to f: a-d-c-f
and a-b-c-f Possible Path from vertex d
to e: d-c-e
Example: Directed Graph

Path from vertex 0 to 7: 0-3-5-6-7


and 0-3-4-6-7
Path Type
Simple Path: A path is simple path if all of its vertices are distinct.
Closed Path: A path is closed path if the first vertex is the same as the last vertex.

Path a-g-f-e-c-f-d-c-b: neither simple nor closed


Path a-g-f-e-c-b: simple Path
Path a-g-f-d-a: closed Path
Types of Graph:
Null Graph
A graph is known as a null Graph if there are no edges in the Graph. In
other words, a graph whose edge set is empty is called a null graph.

Trivial Graph

Graph having only a single vertex and no edges, and it is the smallest possible Graph.
Regular Graph

A graph in which the degree of all the vertices is the same is called a regular graph.
If all the vertices in a graph are of degree 'k', it is called a "k-regular graph."

Connected Graph

The Graph in which we can visit any other node in the Graph from one node is known as a connected graph. At least
one path exists between every pair of vertices in a connected Graph.
Disconnected Graph

The Graph in which at least one node is not reachable from a node is a disconnected graph.

Complete Graph

A graph is a complete Graph in which precisely one edge is present between every pair of vertices.
A complete graph of ‘n’ vertices contains exactly nC2edges.
Cycle Graph

A simple graph of 'n' vertices (n>=3) and n edges forming a cycle of length 'n' is called a cycle graph. In a cycle graph,
all the vertices are of degree 2. In other words, a cycle is a simple closed path.

Cyclic Graph

A graph containing at least one cycle is known a cyclic graph.


Acyclic Graph

A graph not containing any cycle in it is called an acyclic graph.

Multi Graph

A graph that consists of parallel edges and self-loops is called a multigraph. Parallel edges are those which connect the
same pair of vertices, and self-loops have the same vertex as the endpoints
Labelled Graph

A graph that has labels associated with each edge or each vertex is called Labelled Graph.

Weighted Graph A graph having a weight, or number, associated with each edge is called Weighted Graph.
Bi-Partite Graph

A bipartite graph is a set of graph vertices decomposed into two disjoint sets such that no graph vertices
within the same are adjacent. It is also called a bi-graph.
Hamiltonian Graph

A connected graph G is called a Hamiltonian graph if there is a cycle that includes every vertex of G and the obtained
cycle is called the Hamiltonian cycle.

Figure: (a). Hamiltonian Graph (b.) Non-Hamiltonian Graph

Figure (a) graph contains a cycle a-b-c-f-d-e-a that visits each vertex exactly once. Therefore, the cycle is a
Hamiltonian cycle. It implies that the Graph is a Hamiltonian Graph.
A cycle in Figure (b) graph visits all vertices, a-b-d-c-f-d-e-a but vertex d appears twice in the cycle. Thus, the
Graph does not contain a Hamiltonian cycle. It implies that the Graph is a Non-Hamiltonian Graph.
Application of
graph
1. Electrical Engineering- The concepts of graph theory are used extensively in designing circuit connections.
The types or organization of connections are named as topologies.
Some examples for topologies are star, bridge, series and parallel topologies.
2. Computer Science- Graph theory is used for the study of algorithms such
as- Kruskal’s Algorithm
Prim’s Algorithm
Dijkstra’s Algorithm
3.Computer Network- The relationships among interconnected computers in the network follows the principles
of graph theory.
4. Science- Following structures are represented by graphs-
Molecular structure of a substance, Chemical structure of a substance, DNA structure of an organism etc
5. Linguistics- The parsing tree of a language and grammar of a language uses graphs.
6. Other Applications- Routes between the cities are represented using graphs.
Hierarchical ordered information such as family tree are represented using special types of graphs called trees.
Graph
Representation
• There are three types of representation of graphs in the memory
1. Sequential Representation-
The Graph can be represented sequentially by using matrices. It is of two types-
Adjacency –Matrix Representation-
• Adjacency matrix is a sequential representation.
• It is used to represent which nodes are adjacent to each other. i.e. is there any edge connecting nodes to a
graph.
• In this representation, we have to construct a nXn matrix A. If there is any edge from a vertex i to vertex j,
then the corresponding element of A, ai,j = 1, otherwise ai,j= 0.
• If there is any weighted graph then instead of 1s and 0s, we can store the weight of the edge.
Example
Consider the following undirected graph
representation: Undirected graph representation

Directed graph representation

Pros: Representation is easier to implement and follow.


Cons: It takes a lot of space and time to visit all the neighbors of a vertex, we have to traverse
all the vertices in the graph, which takes quite some time.
2. Incidence Matrix
In Incidence matrix representation, graph can be represented using a matrix
of size: Total number of vertices by total number of edges.
It means if a graph has 4 vertices and 6 edges, then it can be represented using a matrix of 4X6
class. In this matrix, columns represent edges and rows represent vertices.
This matrix is filled with either 0 or 1 or -1. Where,
•0 is used to represent row edge which is not connected to column vertex.
•1 is used to represent row edge which is connected as outgoing edge to column vertex.
•-1 is used to represent row edge which is connected as incoming edge to column vertex.

Example
Consider the following directed graph representation.
3. Adjacency List
•Adjacency list is a linked representation.
•In this representation, for each vertex in the graph, we maintain the list of its neighbors. It
means, every vertex of the graph contains list of its adjacent vertices.
•We have an array of vertices which is indexed by the vertex number and for each vertex v, the
corresponding array element points to a singly linked list of neighbors of v.
Example
Let's see the following directed graph representation implemented using linked list:

We can also implement this representation using array as


follows:
Pros:
•Adjacency list saves lot of space.
•We can easily insert or delete as we use linked list.
•Such kind of representation is easy to follow and clearly shows the adjacent nodes of node.
Cons:
•The adjacency list allows testing whether two vertices are adjacent to each other but it is
slower to support this operation.
Graph
Traversal
• Graph traversal is a technique to visit the each nodes of a graph G. It
is also use to calculate the order of vertices in traverse process. We
visit all the nodes starting from one node which is connected to each
other without going into loop.
• Basically in graph it may happen some time visiters can visit one node
more than once. So, this may cause the going the visiters into infinite
loop. So, to protect from this infinite loop condition we keep record of
each vertices. Like if visiters visited vertex then the value will be zero
if not, then one.
There are two graph traversal
techniques
• Breadth-first search
• Depth-first search
Breadth-first
search
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a
graph without loops. We use Queue data structure with maximum size of total
number of vertices in the graph to implement BFS traversal.
We use the following steps to implement BFS traversal...
Step 1 - Define a Queue of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
Exam
ple
Questi
on
1. BFS algorithm has been implemented using the queue data structure. Which one of the
following is a possible order of visiting the nodes in the Graph below?

A MNOPQR
B QMNROP
C NQMPOR
D POQNMR

Ans: (d)
Spanning
tree
• A spanning tree is a subset of Graph G, which has all the vertices covered
with minimum possible number of edges. Hence, a spanning tree does
not have cycles and it cannot be disconnected..
• By this definition, we can draw a conclusion that every connected and
undirected Graph G has at least one spanning tree. A disconnected graph
does not have any spanning tree, as it cannot be spanned to all its
vertices.
Exam
ple
DFS (Depth First
Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without
loops. We use Stack data structure with maximum size of total number of vertices in the graph to
implement DFS traversal.
We use the following steps to implement DFS traversal...
Step 1 - Define a Stack of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the
Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and
push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of
the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the
stack.
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges
from the graph
Exam
ple
Sr. No. Key BFS DFS
BFS, stands for Breadth DFS, stands for Depth
1 Definition
First Search. First Search.
BFS uses Queue to find DFS uses Stack to find
2 Data structure
the shortest path. the shortest path.
BFS is better when target DFS is better when
3 Source
is closer to Source. target is far from source.

DFS is more suitable for


decision tree. As with one
As BFS considers all
decision, we need to
Suitablity for decision neighbour so it is not
4 traverse further to
tree suitable for decision tree
augment the decision. If
used in puzzle games.
we reach the conclusion,
we won.

5 Speed BFS is slower than DFS. DFS is faster than BFS.


Time Complexity of BFS Time Complexity of DFS
6 Time Complexity = O(V+E) where V is is also O(V+E) where V
vertices and E is edges. is vertices and E is edges.
Connected Components
Connected components(G)
for each vertex v
flag[v]=-1
count=0
for( int v=0;v<N; v++)
if (flag[v] = = -1)
DFS(v, flag)
count++
print count;
DFS(int v, int flag)
flag[v]=1;
print v
for each adjacent node u of v
if(flag[u] = = -1)
DFS (u, flag)

You might also like