0% found this document useful (0 votes)
465 views11 pages

Graph Data Structure

The document defines and describes graphs and their components. A graph is made up of vertices (nodes) connected by edges. It can be represented by an adjacency matrix or list. Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are also discussed. DFS uses a stack and traverses depth-wise while BFS uses a queue and traverses breadth-wise.

Uploaded by

sagar krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
465 views11 pages

Graph Data Structure

The document defines and describes graphs and their components. A graph is made up of vertices (nodes) connected by edges. It can be represented by an adjacency matrix or list. Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are also discussed. DFS uses a stack and traverses depth-wise while BFS uses a queue and traverses breadth-wise.

Uploaded by

sagar krishna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Introduction to Graphs

Graph is a non linear data structure; it contains a set of points known as nodes (or vertices) and
set of links known as edges (or Arcs) which connects the vertices. A graph is defined as
follows...

Graph is a collection of vertices and arcs which connects vertices in the graph

or

Graph is a collection of nodes and edges which connects nodes in the graph

Generally, a graph G is represented as G = ( V , E )

where V is set of vertices and E is set of edges.

Example
The following is a graph with 5 vertices and 7 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D), (B,E), (C,D), (D,E)}.

YOGANANDA. A 1
Graph Terminology
We use the following terms in graph data structure...

Vertex

A individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.

Edge

An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph, the link between
vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e., (A,B),
(A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is a undirected


edge between vertices A and B then edge (A , B) is equal to edge (B , A).

2. Directed Edge - A directed egde is a unidirectional edge. If there is a directed edge


between vertices A and B then edge (A , B) is not equal to edge (B , A).

3. Weighted Edge - A weighted egde is an edge with cost on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with undirected and directed edges is said to be mixed graph.

End vertices or Endpoints

The two vertices joined by an edge are called the end vertices (or endpoints) of the edge.

YOGANANDA. A 2
Origin

If an edge is directed, its first endpoint is said to be origin of it.

Destination
If an edge is directed, its first endpoint is said to be origin of it and the other endpoint is said to
be the destination of the edge.

Adjacent

If there is an edge between vertices A and B then both A and B are said to be adjacent. In other
words, Two vertices A and B are said to be adjacent if there is an edge whose end vertices are A
and B.

Incident

An edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.

Outgoing Edge

A directed edge is said to be outgoing edge on its orign vertex.

Incoming Edge

A directed edge is said to be incoming edge on its destination vertex.

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.

Indegree

Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Outdegree

Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges

If there are two undirected edges to have the same end vertices, and for two directed edges to
have the same origin and the same destination. Such edges are called parallel edges or multiple
edges.

YOGANANDA. A 3
Self-loop

An edge (undirected or directed) is a self-loop if its two endpoints coincide.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternating vertices and edges that starts at a vertex and ends at a vertex
such that each edge is incident to its predecessor and successor vertex.

Graph Representations
Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

Adjacency Matrix
In this representation, graph can be represented using a matrix of size total number of vertices by
total number of vertices. That means if a graph with 4 vertices can be represented using a matrix
of 4X4 class. In this matrix, rows and columns both represents vertices. This matrix is filled with
either 1 or 0. Here, 1 represents there is an edge from row vertex to column vertex and 0
represents there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...

YOGANANDA. A 4
Directed graph representation...

Incidence Matrix
In this representation, graph can be represented using a matrix of size total number of vertices by
total number of edges. That means if a graph with 4 vertices and 6 edges can be represented
using a matrix of 4X6 class. In this matrix, rows represents vertices and columns represents
edges. This matrix is filled with either 0 or 1 or -1. Here, 0 represents row edge is not connected
to column vertex, 1 represents row edge is connected as outgoing edge to column vertex and -1
represents row edge is connected as incoming edge to column vertex.

For example, consider the following directed graph representation...

Adjacency List
In this representation, every vertex of graph contains list of its adjacent vertices.

For example, consider the following directed graph representation implemented using linked
list...

YOGANANDA. A 5
This representation can also be implemented using array as follows..

Depth First Traversal


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.

YOGANANDA. A 6
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

1 Initialize the stack.

Mark S as visited and put it onto the stack.


Explore any unvisited adjacent node from
2 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
3 node from A. Both S and D are adjacent
to A but we are concerned for unvisited
nodes only.

YOGANANDA. A 7
Visit D and mark it as visited and put onto
the stack. Here, we have B and C nodes,
4 which are adjacent to D and both are
unvisited. However, we shall again choose
in an alphabetical order.

We choose B, mark it as visited and put


onto the stack. Here B does not have any
5
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
6
unvisited nodes. Here, we find D to be on
the top of the stack.

Only unvisited adjacent node is from D is


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

YOGANANDA. A 8
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.

Breadth First Traversal


Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a
queue 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, BFS algorithm traverses from A to B to E to F first then to C and
G lastly to D. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a
queue.

 Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.

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

YOGANANDA. A 9
Step Traversal Description

1 Initialize the queue.

We start from visiting S (starting node),


2
and mark it as visited.

We then see an unvisited adjacent node


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

Next, the unvisited adjacent node from S


4 is B. We mark it as visited and enqueue
it.

YOGANANDA. A 10
Next, the unvisited adjacent node from S
5 is C. We mark it as visited and enqueue
it.

Now, S is left with no unvisited adjacent


6
nodes. So, we dequeue and find A.

From A we have D as unvisited adjacent


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

YOGANANDA. A 11

You might also like