0% found this document useful (0 votes)
49 views35 pages

Graphs: Concepts and Operations Guide

Uploaded by

jsubhra502
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)
49 views35 pages

Graphs: Concepts and Operations Guide

Uploaded by

jsubhra502
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

Data Structures and Algorithms (CS-2001)

KALINGA INSTITUTE OF INDUSTRIAL


TECHNOLOGY

School of Computer Engineering

Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission

4 Credit Lecture Note


Chapter Contents
2

Sr # Major and Detailed Coverage Area Hrs

6 Graphs 5
Graph ADT, Graph Operation – DFS, BFS

School of Computer Engineering


Introduction
3

A graph is a representation of a set of objects where some pairs of objects are


connected by links. The interconnected objects are represented by points
termed as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is
the set of edges, connecting the pairs of vertices. Take a look at the following
graph −

In the above graph,


V = {a, b, c, d, e}
E = {(a,b), (a,c), (b,d), (c,d), (d,e)}

School of Computer Engineering


Formal Definition
4

A graph G is defined as follows:


G = (V, E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)

Class Work

Define V(G) and E(G) of the above graph


School of Computer Engineering
Directed and Undirected Graph
5

Directed Graph Undirected Graph


When the edges in a graph have no When the edges in a graph have a
direction, the graph is called direction, the graph is called directed (or
undirected digraph)

School of Computer Engineering


Trees vs. Graphs
6

Trees are special cases of graphs!!

School of Computer Engineering


Graph Terminology
7

 Adjacent nodes: two nodes are adjacent if they are connected by an edge
5 is adjacent to 7
5 7
7 is adjacent from 5
 Path: a sequence of vertices that connect two nodes in a graph
 Complete graph: a graph in which every vertex is directly connected to every other
Subtree
vertex

What is the number of edges in a complete directed What is the number of edges in a complete undirected
graph with N vertices? graph with N vertices?

School of Computer Engineering


Graph Terminology cont…
8

 Loop: In a graph, if an edge is drawn from vertex to itself, it is called a loop.

 Degree of Vertex: The degree of a graph vertex of a graph is the number of graph
Subtree
edges which touch.
Degree of vertex in undirected graph Degree of vertex in directed graph
Each vertex has an indegree and an outdegree. Indegree of vertex V is the
number of edges which are coming into the vertex V. Outdegree of
vertex V is the number of edges which are going out from the vertex V.
Vertex Indegree Outdegree
a 1 1
b 0 2
c 2 0

deg(a) = 2, deg(b) = 2, deg(c) = 2, d 1 1


e 1 1
deg(d) = 2, and deg(e) = 0
School of Computer Engineering
Graph Terminology cont…
9

 Pendent Vertex: A vertex with degree one is called a pendent vertex.

 Isolated Vertex: A vertex with degree zero is called an isolated vertex.

Subtree
 Parallel Edges: In a graph, if a pair of vertices is connected by more than one edge,
then those edges are called parallel edges.

 Multi Graph: A graph having parallel edges is known as a Multigraph.

School of Computer Engineering


Graph Terminology cont…
10

Weighted graph: a graph in which each edge carries a value

Subtree

School of Computer Engineering


Graph Terminology cont…
11

 Directed Acyclic graph: a direct graph with no cycles

Subtree

 Bi-connected graph: It is a connected graph which cannot be broken down into any
further pieces by deletion of any single vertex and incident edges.

School of Computer Engineering


Representing Graphs in Memory
12

Let G be the graph. There are 2 ways of representing it in memory. The first way is called
the link representation of G and the second way which uses arrays is called the
sequential representation of G.
Sequential Representation (Array based)
 A 1D array is used to represent the vertices
 A 2D array (adjacency matrix) is used to represent the edges

School of Computer Engineering


Class Work
13

Let G be the unweighted undirected Let G’ be the unweighted directed graph


graph with 5 vertices. Represent it using with 5 vertices. Represent it using
Adjacency Matrix Adjacency Matrix

School of Computer Engineering


Linked Representation of Graph
14

 A 1D array is used to represent the vertices


 A list is used for each vertex v which contains the vertices which are adjacent from v
(adjacency list)

School of Computer Engineering


Class Work
15

Let G be the unweighted undirected Let G’ be the unweighted directed graph


graph with 5 vertices. Represent it using with 5 vertices. Represent it using
Adjacency List Adjacency List

School of Computer Engineering


Adjacency List vs. Adjacency Matrix
16

Adjacency Matrix Adjacency List


 Representation of graphs is very simple to  Very memory efficient when the graph has a large
implement. number of vertices but very few edges.
 For an undirected graph with n vertices and e edges,
 Wastes lot of memory space. Such matrices
total number of nodes will be n + 2e. If e is large
are found to be very sparse. This
then due to overhead of maintaining pointers,
representation requires space for n2 adjacency list representation does not remain cost
elements for a graph with n vertices. effective over adjacency matrix representation of a
 Degree of a vertex can easily be calculated graph.
by counting all non-zero entries in the  Degree of a node in an undirected graph is given by
corresponding row of the adjacency the length of the corresponding linked list.
 Finding indegree of a directed graph represented
matrix.
using adjacency list will require O (e) comparisons.
 Presence of an edge between two vertices
Lists pointed by all vertices must be examined to
Vi and Vj can be checked in constant time. find the indegree of a node in a directed graph.
if(adj[i][j] == 1) then edge is present  Checking the existence of an edge between two
between vertices i and j, else edge is absent vertices i and j is also time consuming. Linked list of
between vertices i and j vertex i must be searched for the vertex j.

School of Computer Engineering


Graph Theory Application
17

 Electrical Engineering − The concepts of graph theory is 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.
 Computer Network − The relationships among interconnected computers in the
Subtree
network follows the principles of graph theory.
 Science − The molecular structure and chemical structure of a substance, the DNA
structure of an organism, etc., are represented by graphs.
 Linguistics − The parsing tree of a language and grammar of a language uses graphs.
 General − Routes between the cities can be represented using graphs. Depicting
hierarchical ordered information such as family tree can be used as a special type of
graph called tree.
 Computer Science − Graph theory is used for the study of algorithms. For example,
Kruskal's Algorithm, Prim's Algorithm and Dijkstra's Algorithm

School of Computer Engineering


Graph Operation
18

 Search a vertex and the edge between two vertices


 Inserting a vertex
 Inserting edges between two vertices
 Deleting a vertex
 Deleting edges between two vertices
Subtree
 Traversal
 Breadth-First Search
 Depth-First Search

School of Computer Engineering


Breadth-First Search
19

Breadth-First Search algorithm(BFS) traverses a graph in a breadth wards motion and uses a queue
to remember to get the next vertex to start a search when a dead end occurs in any iteration.
Start Algorithm Rule
Procedure BFS(graph G)
begin
 Rule 1 − Visit the adjacent
Queue Q; unvisited vertex. Mark it as
char s, x;
visited. Display [Link]
Insert it in
while (G has an unvisited node) do
s = an unvisited node; a queue.
visit(s);  Rule 2 − If no adjacent
Enqueue(s, Q);
While (Q is not empty) do
vertex is found, remove the
x = Dequeue(Q); first vertex from the queue.
For (unvisited neighbor y of x) do  Rule 3 − Repeat Rule 1 and
visit(y);
Enqueue(y, Q); Rule 2 until the queue is
endfor empty.
endwhile
endwhile
end
School of Computer Engineering
Breadth-First Search Example
20

Step # Traversal Description O/P Seq


1 Initialize the Queue --

Subtree

2 We start from visiting S (starting node), S


and mark it visited and put it onto the
queue

School of Computer Engineering


Breadth-First Search Example cont…
21

Step # Traversal Description O/P Seq


3 Queue is not empty and S is dequed. S, A
We then see unvisited adjacent node
from S. In this example, we have three
nodes but alphabetically we choose A
mark it visited and enqueue it. Subtree

4 Next unvisited adjacent node from S is S, A, B


B. We mark it visited and enqueue it.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Breadth-First Search Example cont…
22

Step # Traversal Description O/P Seq


5 Next unvisited adjacent node from S is S, A, B, C
C. We mark it visited and enqueue it.

Subtree

6 Now S is left with no unvisited adjacent S, A, B, C


nodes. So we dequeue and find A.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Breadth-First Search Example cont…
23

Step # Traversal Description O/P Seq


7 From A we have D as unvisited S, A, B, C,
adjacent node. We mark it visited and D
enqueue it.

Subtree

8 At this stage we are left with no S, A, B, C,


unmarked (unvisited) nodes. But as per D
algorithm we keep on dequeuing in
order to get all unvisited nodes. When
the queue gets emptied the algorithm
is over.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Depth-First Search
24
Depth-First Search algorithm(DFS) traverses a graph in a depth-ward motion and uses a stack to
remember to get the next vertex to start a search when a dead end occurs in any iteration
Start Algorithm Rule
Procedure DFS(graph G)
 Rule 1 − Visit the adjacent
begin
Stack S; unvisited vertex. Mark it as
char s,x; visited. Display it. Push it in a
while (G has an unvisited node) do Subtree
s = an unvisited node; stack.
visit(v);  Rule 2 − If no adjacent vertex
push(v,S);
While (S is not empty) do
is found, pop up a vertex from
x = top(S); the stack. (It will pop up all
if (x has an unvisited neighbor y) then
the vertices from the stack,
visit(y);
push(y, S); which do not have adjacent
else vertices.)
pop(S);
endif
 Rule 3 − Repeat Rule 1 and
endwhile Rule 2 until the stack is empty.
endwhile
end
School of Computer Engineering
Depth-First Search Example
25

Step # Traversal Description O/P Seq


1 Initialize the stack --

Subtree

2 Mark S as visited and put it onto the S


stack. Explore any unvisited adjacent
node from S. We have three nodes and
we can pick any of them. For this
example, we shall take the node in
alphabetical order.

School of Computer Engineering


Depth-First Search Example
26

Step # Traversal Description O/P Seq


3 Mark A as visited and put it onto the S, A
stack. Explore any unvisited adjacent
node from A. Both S and D are
adjacent to A but we are concerned
for unvisited nodes only. Subtree

4 Visit D and mark it visited and put S, A, D


onto the stack. Here we have B and C
nodes which are adjacent to D and
both are unvisited. But we shall again
choose in alphabetical order.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Depth-First Search Example
27

Step # Traversal Description O/P Seq


5 We choose B, mark it visited and put S, A, D, B
onto stack. Here B does not have any
unvisited adjacent node. So we pop B
from the stack.
Subtree

6 We check stack top for return to S, A, D, B


previous node and check if it has any
unvisited nodes. Here, we find D to be
on the top of stack.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Depth-First Search Example
28

Step # Traversal Description O/P Seq


7 Only unvisited adjacent node is from S, A, D, B,
D is C now. So we visit C, mark it C
visited and put it onto the stack.

Subtree

8 As C does not have any unvisited S, A, D, B,


adjacent node so we keep popping C
the stack until we find a node which
has unvisited adjacent node. In this
case, there's none and we keep
popping until stack is empty.

Note: comma used in Output Sequence (i.e. O/P Seq) is for illustration

School of Computer Engineering


Class Work
29

Display O/P sequence using DFS and BFS method for each Graph
Start

Start

Start

Start

School of Computer Engineering


30

School of Computer Engineering


Home Work (HW)
31

 Write a Program using DFS method for directed graph traversal


 Write a Program using BFS method for directed graph traversal
 Write an algorithm that traverse the graph. If the graph is connected, it should return
true and should return false if the graph is disjoint
 Write Pseudo code for the following undirected graph. Assume graph is represented
in memory in adjacency matrix format
 Initialize the graph with new node named ‘A’ and ‘B’
 Add edge between ‘A’ and ‘B’
 Insert a new nodes i.e. ‘C’, ‘D’ and ‘E’ to the Graph
 Insert edge between ‘C’ & ‘D’ and ‘A’ & ‘D’ and ‘B’ & ‘C’
 DFS
 BFS
 Search for the node ‘B’ and display the position of the node and its connecting
nodes
 Delete the edge between ‘C’ & ‘D’
 Delete the node ‘B’
 Search for the node ‘D’ and display the position of the node and its connecting
nodes
School of Computer Engineering
Home Work (HW) cont…
32

 Write a procedure which determines whether or not G is an undirected graph


 Write a procedure to
 Print the list of successors of the given node N in the undirected graph G
 Print the list of successors of the given node N in the directed graph G
 Print the list of predecessors of the given node N in the undirected graph G’
 Print the list of predecessors of the given node N in the directed graph G’
 Show that the sum of the degrees of the vertices of an undirected graph is twice the
number of edges

School of Computer Engineering


Supplementary Reading
33

 [Link]
 [Link]
 [Link]
 [Link]
 [Link]

School of Computer Engineering


FAQ
34
Describe Graphs with example.
Graphs are widely-used structure in computer science and different computer applications. Graphs
mean to store and analyze metadata, the connections, which present in data. For instance, consider
cities in a country. Road network, which connects them, can be represented as a graph and then
analyzed. We can examine, if one city can be reached from another one or find the shortest route
between two cities. There are two important sets of objects, which specify graph and its structure.
First set is V, which is called vertex-set. In the example with road network cities are vertices. Each
vertex can be drawn as a circle with vertex's number inside. Next important set is E, which is called
edge-set. E is a subset of V x V. Simply speaking, each edge connects two vertices, including a case,
when a vertex is connected to itself (such an edge is called a loop). All graphs are divided into two big
groups: directed and undirected graphs. The difference is that edges in directed graphs, called arcs,
have a direction. These kinds of graphs have much in common with each other, but significant
differences are also present. We will accentuate which kind of graphs is considered in the particular
algorithm description. Edge can be drawn as a line. If a graph is directed, each line has an arrow.
Vertices Undirected Graph Directed Graph

School of Computer Engineering


FAQ
35

BFS: DFS:
BFS is a traversing algorithm where you The DFS is a recursive algorithm uses the
should start traversing from a selected idea of backtracking. It involves exhaustive
node (source or starting node) and searches of all the nodes by going ahead, if
traverse the graph layer-wise thus possible, else by backtracking.
exploring the neighbor nodes (nodes
which are directly connected to source Here, the word backtrack means that when
node). You must then move towards the you are moving forward and there are no
next-level neighbor nodes. more nodes along the current path, you
move backwards on the same path to find
As the name BFS suggests, you are nodes to traverse. All the nodes will be
required to traverse the graph breadth- visited on the current path till all the
wise. unvisited nodes have been traversed after
which the next path will be selected.
It uses a queue to remember to get the
next vertex to start a search when a dead This recursive nature of DFS can be
end occurs in any iteration implemented using stacks.

School of Computer Engineering

You might also like