0% found this document useful (0 votes)
9 views

Graph Notes

Uploaded by

Jayalakshmy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Graph Notes

Uploaded by

Jayalakshmy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT V

Introduction

A graph is a mathematical structure consisting of a set of vertices (also called


nodes) {v1,v2,…,vn) and a set of edges {e1,e2,…,en}. An edge is a pair of vertices {vi,vj} i,j ∈
{1…n}. Graphs are ubiquitous in computer science.
Formally: G = (V, E), where V is a set and E ⊆ V × V

They are used to model real-world systems such as the Internet (each node represents a router and
each edge represents a connection between routers); airline connections (each node is an airport
and each edge is a flight); or a city road network (each node represents an intersection and each
edge represents a block). The wireframe drawings in computer graphics are another example of
graphs.
Types of Graph
 Directed
 Undirected
 Weighted

A graph may be either undirected or directed. Intuitively, an undirected edge models a "two-way"
or "duplex" connection between its endpoints, while a directed edge is a one-way connection, and
is typically drawn as an arrow. A directed edge is often called an arc. Mathematically, an
undirected edge is an unordered pair of vertices, and an arc is an ordered pair.

Fig1:Undirect Graph Fig2:Direct Graph

Graphs can be classified by whether or not their edges have weights. In Weighted graph, edges
have a weight. Weight typically shows cost of traversing
Example: weights are distances between cities

In Unweighted graph, edges have no weight. Edges simply show connections.


Weighted graph

 Degree of a vertex, v, denoted deg(v) is the number of incident edges.


Out-degree, outdeg(v), is the number of outgoing edges.
In-degree, indeg(v), is the number of incoming edges.
 Path is a sequence of alternating vetches and edges such that each successive vertex is
connected by the edge.
 Acyclic graph: Finite directed graph with no directed cycles

GRAPH REPRESENTATIONS
There are two standard ways of maintaining a graph G in the memory of a computer.

1. The sequential representation


2. The linked representation

SEQUENTIAL REPRESENTATION OF GRAPHS


Adjacency Matrix Representation
An adjacency matrix is one of the two common ways to represent a graph. The adjacency matrix
shows which nodes are adjacent to one another. Two nodes are adjacent if there is an edge
connecting them. In the case of a directed graph, if node j is adjacent to node i, there is an edge
from i to j . In other words, if j is adjacent to i, you can get from i to j by traversing one edge. For
a given graph with n nodes, the adjacency matrix will have dimensions of nxn. For an unweighted
graph, the adjacency matrix will be populated with Boolean values.
For any given node i, you can determine its adjacent nodes by looking at row (i,[1…n]) adjacency
matrix. A value of true at (i,j ) indicates that there is an edge from node i to node j, and false
indicating no edge. In an undirected graph, the values of (i,j) and (j,i)will be equal. In a weighted
graph, the boolean values will be replaced by the weight of the edge connecting the two nodes,
with a special value that indicates the absence of an edge.

The memory use of an adjacency matrix is O(n2).


·
·
· Example undirected graph (assume self-edges not allowed):
·· A B C D
·
A0 1 1 1
·
·
B1 0 0 1
·
·
C1 0 0 1
· D1 1 1 0

· Example directed graph (assume self-edges allowed):

A B C D
A 0 1 1 1
B 0 0 0 1
C 0 0 0 0
D 0 0 1 0
LINKED LIST REPRESENTATION OF GRAPH

Adjacency List Representation

The adjacency list is another common representation of a graph. There are many ways to
implement this adjacency representation. One way is to have the graph maintain a list of lists, in
which the first list is a list of indices corresponding to each node in the graph. Each of these refer
to another list that stores a the index of each adjacent node to this one. It might also be useful to
associate the weight of each link with the adjacent node in this list.
Example: An undirected graph contains four nodes 1, 2, 3 and 4. 1 is linked to 2 and 3. 2 is linked
to 3. 3 is linked to 4.
1 - [2, 3]

2 - [1, 3]

3 - [1, 2, 4]

4 - [3]

Adjacency list for Directed Graph


Adjacency list for Undirected Graph

Besides that,we can also represent graph using set representation


In set representation,we can represent the following graph as follows

Here we have G=(V,E)


V(G)={1,2,3,4}
E(G)={(1,3),(1,4),(3,4),(2,4)}
GRAPH TRAVERSAL
Graph traversal is the problem of visiting all the nodes in a graph in a particular manner, updating
and/or checking their values along the way. The order in which the vertices are visited may be
important, and may depend upon the particular algorithm.
The two common traversals:

breadth-first

depth-first

Breadth First Search

The bread-first-search algorithm starts at a vertex and visits, first the neighbours of , then the
neighbours of the neighbours of , then the neighbours of the neighbours of the neighbours of
, and so on. This algorithm is a generalization of the breadth-first traversal algorithm for binary
trees. It uses a queue.

• A breadth-first search (BFS) explores


nodes nearest the root before exploring
nodes further away
• For example, after searching A, then B,
then C, the search proceeds with D, E,
F, G
• Node are explored in the order A B C
DEFGHIJKLMNOPQ
• J will be found before N

Algorithm_BFS

1. Initialize an array A and a queue Q.


2. Read the vertex Vi from which you want to start the traversal.
3. Initialize the index of the array equal to 1 at the index of Vi
4. Insert the vertex Vi into the queue Q
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(if at that index in the array the entry is not equal to 1)
6. Repeat step 5 till the queue Q is empty
Depth First Search

The depth-first-search algorithm is similar to the standard algorithm for traversing binary trees; it
first fully explores one subtree before returning to the current node and then exploring the other
subtree. Another way to think of depth-first-search is by saying that it is similar to breadth-first
search except that it uses a stack instead of a queue.

• Eg. A depth-first search (DFS)


explores a path all the way to a leaf
before backtracking and exploring
another path

• For example, after searching A, then


B, then D, the search backtracks and
tries another path from B

• Node are explored in the order A B D


EHLMNIOPCFGJK
• N will be found before J

Algorithm_DFS

1. Initialize an array A and a stack S.


2. Read the vertex Vi from which you want to start the traversal.
3. Initialize the index of the array equal to 1 at the index of Vi
4. Insert the vertex Vi into the Stack S
5. Visit the vertex which is at the top of the stack. Delete it from the queue and place its
adjacent nodes in the stack(if at that index in the array the entry is not equal to 1)
6. Repeat step 5 till the stack S is empty.

Example of the DFS algorithm


 Example:

Graph:
Initial state: node 0 is pushed

State after visiting 0

Push the unvisited neighbor nodes: 8, 3, 1 (I used the reverse order to visit smaller node id first)
Next, visit the top node in the stack: 1

State after visiting 1


Push the unvisited neighbor nodes: 7
Next, visit the top node in the stack: 7

State after visiting 7

Push the unvisited neighbor nodes: 2


Next, visit the top node in the stack: 2

State after visiting 2


Push the unvisited neighbor nodes: 5, 3 (Note: 3 is pushed again, and the previous value will be cancelled later -- as we will s
Next, visit the top node in the stack: 3

State after visiting 3

Push the unvisited neighbor nodes: 4


Next, visit the top node in the stack: 4

State after visiting 4

Push the unvisited neighbor nodes: 8 (Note: 8 is pushed again, and the previous value will be cancelled later -- as we will see
Next, visit the top node in the stack: 8

State after visiting 8


Push the unvisited neighbor nodes: none
Next, visit the top node in the stack: 5

State after visiting 5

Push the unvisited neighbor nodes: 6


Next, visit the top node in the stack: 6

State after visiting 6

Push the unvisited neighbor nodes: none


Next, visit the top node in the stack: 3

 3 is visited: skip

Result:

Next, visit the top node in the stack: 8

 8 is visited: skip

Result:

DONE
(The stack has become empty)

mple of the BFS algorithm

Example:

 Graph:


 Initial state: node 0 is enqueued


 State after visiting 0
 Enqueue the unvisited neighbor nodes: 1, 3, 8
 Next, visit the first node in the queue: 1

 State after visiting 1

 Enqueue the unvisited neighbor nodes: 7


 Next, visit the first node in the queue: 3

 State after visiting 3
 Enqueue the unvisited neighbor nodes: 2, 4
 Next, visit the first node in the queue: 8

 State after visiting 8

 Enqueue the unvisited neighbor nodes: none (Note: 4 is enqueued again, but won't be visited twice, so I leave
out)
 Next, visit the first node in the queue: 7

 State after visiting 7

 Enqueue the unvisited neighbor nodes: none (Note: 2 is enqueued again, but won't be visited twice, so I leave
out)
 Next, visit the first node in the queue: 2

 State after visiting 2
 Enqueue the unvisited neighbor nodes: 5
 Next, visit the first node in the queue: 4

 State after visiting 4

 Enqueue the unvisited neighbor nodes: none


 Next, visit the first node in the queue: 5

 State after visiting 5
 Enqueue the unvisited neighbor nodes: 6
 Next, visit the first node in the queue: 6

 State after visiting 6

You might also like