0% found this document useful (0 votes)
3 views16 pages

graph-1

A graph is a data structure consisting of vertices (nodes) and edges (connections), represented as a pair of sets (V, E). Graphs can be directed or undirected, and can be represented using adjacency matrices or adjacency lists, each with its own advantages and disadvantages. Graph traversal techniques include Depth First Search (DFS) and Breadth First Search (BFS), which are used to explore the vertices of a graph systematically.

Uploaded by

pespsyco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

graph-1

A graph is a data structure consisting of vertices (nodes) and edges (connections), represented as a pair of sets (V, E). Graphs can be directed or undirected, and can be represented using adjacency matrices or adjacency lists, each with its own advantages and disadvantages. Graph traversal techniques include Depth First Search (DFS) and Breadth First Search (BFS), which are used to explore the vertices of a graph systematically.

Uploaded by

pespsyco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Graph Data Structure

A graph is a pictorial 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 = {ab, ac, bd, cd, de}

Graph Data Structure


Mathematical graphs can be represented in data structure. We can
represent a graph using an array of vertices and a two-dimensional array of
edges. Before we proceed further, let's familiarize ourselves with some
important terms −

 Vertex − Each node of the graph is represented as a vertex. In the following


example, the labeled circle represents vertices. Thus, A to G are vertices. We
can represent them using an array as shown in the following image. Here A can
be identified by index 0. B can be identified using index 1 and so on.
 Edge − Edge represents a path between two vertices or a line between two
vertices. In the following example, the lines from A to B, B to C, and so on
represents edges. We can use a two-dimensional array to represent an array as
shown in the following image. Here AB can be represented as 1 at row 0, column
1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0.
 Adjacency − Two node or vertices are adjacent if they are connected to each
other through an edge. In the following example, B is adjacent to A, C is
adjacent to B, and so on.
 Path − Path represents a sequence of edges between the two vertices. In the
following example, ABCD represents a path from A to D.

Directed Graph

 If a graph contains ordered pair of vertices, is said to be a Directed Graph.


 If an edge is represented using a pair of vertices (V 1, V2), the edge is said to be
directed from V1 to V2.
 The first element of the pair V1 is called the start vertex and the second
element of the pair V2 is called the end vertex.

Set of Vertices V = {1, 2, 3, 4, 5, 5}


Set of Edges W = {(1, 3), (1, 5), (2, 1), (2, 3), (2, 4), (3, 4), (4, 5)}
Undirected Graph

 If a graph contains unordered pair of vertices, is said to be an Undirected


Graph.
 In this graph, pair of vertices represents the same edge.

Set of Vertices V = {1, 2, 3, 4, 5}


Set of Edges E = {(1, 2), (1, 3), (1, 5), (2, 1), (2, 3), (2, 4), (3, 4), (4, 5)}

 In an undirected graph, the nodes are connected by undirected arcs.


 It is an edge that has no arrow. Both the ends of an undirected arc are
equivalent; there is no head or tail.

Graph can be divided into two categories:


a. Sparse Graph
b. Dense Graph
a. Sparse graph contains less number of edges.
b. Dense graph contains number of edges as compared to sparse graph.

Representation of Graphs

Adjacency Matrix
 Adjacency matrix is a way to represent a graph.
 It shows which nodes are adjacent to one another.
 Graph is represented using a square matrix.
 Adjacency matrix is best for dense graph, but for sparse graph, it is not
required.
 Adjacency matrix is good solution for dense graph which implies having
constant number of vertices.
 Adjacency matrix of an undirected graph is always a symmetric matrix which
means an edge (i, j) implies the edge (j, i).
 The above graph represents undirected graph with the adjacency matrix
representation. It shows adjacency matrix of undirected graph is symmetric. If
there is an edge (2, 4), there is also an edge (4, 2).

 Adjacency matrix of a directed graph is never symmetric adj[i][j] = 1, indicated


a directed edge from vertex i to vertex j.

 The above graph represents directed graph with the adjacency matrix
representation. It shows adjacency matrix of directed graph which is never
symmetric. If there is an edge (2, 4), there is not an edge (4, 2). It indicates
direct edge from vertex i to vertex j.
Advantages of Adjacency Matrix

 Adjacency matrix representation of graph is very simple to implement.


 Adding or removing time of an edge can be done in O(1) time. Same time is
required to check, if there is an edge between two vertices.
 It is very convenient and simple to program.
Disadvantages of Adjacency Matrix

 It consumes huge amount of memory for storing big graphs.


 It requires huge efforts for adding or removing a vertex. If you are constructing
a graph in dynamic structure, adjacency matrix is quite slow for big graphs.
Adjacency List
 Adjacency list is another representation of graphs.
 It is a collection of unordered list, used to represent a finite graphs.
 Each list describes the set of neighbors of a vertex in the graph.
 Adjacency list requires less amount of memory.
 For every vertex, adjacency list stores a list of vertices, which are adjacent to
the current one.
 In adjacency list, an array of linked list is used. Size of the array is equal to the
number of vertices.

 In adjacency list, an entry array[i] represents the linked list of vertices adjacent
to the ith vertex.
 Adjacency list allows to store the graph in more compact form than adjacency
matrix.
 It allows to get the list of adjacent vertices in O(1) time.
Disadvantages of Adjacency List
 It is not easy for adding or removing an edge to/from adjacent list.
 It does not allow to make an efficient implementation, if dynamically change of
vertices number is required.

Basic Operations
Following are basic primary operations of a Graph −

 Add Vertex − Adds a vertex to the graph.


 Add Edge − Adds an edge between the two vertices of the graph.
 Display Vertex − Displays a vertex of the graph.

Graph Traversal

 Graph traversal is a process of checking or updating each vertex in a graph.


 It is also known as Graph Search.
 Graph traversal means visiting each and exactly one node.
 Tree traversal is a special case of graph traversal.

There are two techniques used in graph traversal:

1. Depth First Search


2. Breadth First Search

1. Depth First Search


 Depth first search (DFS) is used for traversing a finite graph.
 DFS traverses the depth of any particular path before exploring its breadth.
 It explores one subtree before returning to the current node and then exploring
the other subtree.
 DFS uses stack instead of queue.
 It traverses a graph in a depth-ward motion and gets the next vertex to start a
search when a dead end occurs in any iteration.

Algorithm
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

Depth First Search (DFS) algorithm traverses a graph in a depthward motion


and uses a stack to remember to get the next vertex to start a search, when
a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to D to


G to E to B first, then to F and lastly to C. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it
in a stack.

 Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will
pop up all the vertices from the stack, which do not have adjacent vertices.)

 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.

Step Traversal Description

Initialize the stack.


2

Mark S as visited and put it onto the


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 an
alphabetical order.

Mark A as visited and put it onto the


stack. Explore any unvisited adjacent
node from A. Both S and D are adjacent
to A but we are concerned for unvisited
nodes only.

Visit D and mark it as visited and put


onto the stack. Here, we
have B and Cnodes, which are adjacent
to D and both are unvisited. However,
we shall again choose in an alphabetical
order.
5

We choose B, mark it as visited and put


onto the stack. Here B does not have
any unvisited adjacent node. So, we
pop B from the stack.

We check the stack top for return to the


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

Only unvisited adjacent node is


from Dis C now. So we visit C, mark it
as visited and put it onto the stack.

As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node. In this case,
there's none and we keep popping until the stack is empty.

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

Ste Traversal Description


p
1

Initialize the queue.

We start from visiting S (starting node),


and mark it as visited.

We then see an unvisited adjacent node


from S. In this example, we have three
nodes but alphabetically we choose A,
mark it as visited and enqueue it.

Next, the unvisited adjacent node


from S is B. We mark it as visited and
enqueue it.
5

Next, the unvisited adjacent node


from S is C. We mark it as visited and
enqueue it.

Now, S is left with no unvisited adjacent


nodes. So, we dequeue and find A.

From A we have D as unvisited adjacent


node. We mark it as visited and enqueue
it.

At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm
we keep on dequeuing in order to get all unvisited nodes. When the queue gets
emptied, the program is over.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy