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

Data Stracture & Algorithm Chapter Eight Graph

Uploaded by

gadisakarorsa
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)
23 views45 pages

Data Stracture & Algorithm Chapter Eight Graph

Uploaded by

gadisakarorsa
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/ 45

CHAPTER SEVEN

GRAPHS

Wednesday, May 18, 2011


GRAPHS

December 20, 2010


What is Graphs?
 A graph is a mathematical abstraction used to represent
"connectivity information".
 A graph G consists of a set of V or vertices (nodes) and a set if
edges (arcs).
 We write G=(V,E).
 V is a finite non empty set of vertices.
 E is a set of pairs of vertices. These pairs are called edges
What is Graphs...
 An edge e = (v,w), is a pair of vertices v and w, and is said to be incident
with v and w.
1

2 5

3 4

 V(G)= { 1,2,3,4,5}
 E(G)={ (1,2), (2,3), (3,4), (4,5), (1,5), (1,3), (3,5) }
Type of Graph
 There is two type of graphs
1. undirected graph.
 In an undirected graph, pair of vertices representing any edge
is unordered.
 Thus (v,w) and (w,v) represent the same edge.
1

2 5

 V(G)= { 1,2,3,4,5} 3 4

 E(G)={ (1,2), (2,3), (3,4), (4,5), (1,5), (1,3), (3,5) }


 We can also have written (5,1) instead of (1,5).
 The same applies to all the other edges.
continued
Type of Graph …
2. A Directed Graph
 Each edge is an ordered pairs of vertices, i.e. each edge is represented by
a directed pair.
 If e = (v,w) then v is tail or initial vertex and w is head of final vertex.
 Subsequently (v,w) and (w,v) represent two different edges.
1

2 5

 The direction is indicated by arrow.


 V(G) = {1,2,3,4,5}
3 4
 E(G) = { (1,2), (2,3), (3,4), (5,3), (5,4), (5,1) }
 Directed graph can be referred as digraph and
 undirected graph as graph.
Graphs …
 Adjacent Vertices
 Vertex v1 is said to be adjacent to vertex v2 if there is an edge (v1,
v2) or (v2, v1). 1

3 2

7
 Ex: 5 6 4

 Vertices adjacent to node 3 are 1,5,6,4


 Vertices adjacent to node 2 are 1 and 7

 Finite Graph:
 Graph with finite number of nodes and finite number of
edges.
Graphs …
 Loop:
 An edge with identical end points is called a loop.
 Multiple edges:
 The edges connected the same end points.
 Multi-Graph:
 A graph with multiple edges. For multi graphs, even though, there
are finite number of nodes, the edges may not be finite.
Graphs …
 Path:
 A path from vertex v to vertex w is a sequence of vertices, each
adjacent to the next.
1
1,3,4 is a path
1,3,6 is a path 3 2

1,2,7 is a path
7
1,3,4,7 is a path 5 6 4

 We may notice that there is a path which starts at vertex 1 and


finishes at vertex 1. i.e. path 1,3,4,7,2,1. Such a path is called
a cycle.
 Cycle:- A cycle is a path in which first and last vertices are the
same.
Graphs …
 Weighted graphs:
 Sometimes, we include a "weight" (number) with each edge.
 Weight can signify length (for a geometric application) or
"importance".
Graphs …
 Connected Graph
 A graph is called 'connected' , if there exists a path between
any two of its nodes. The above graph is a connected graph.
1

2
A weakly connected graph

4 5

 There does not exist a directed path from vertex 1 to vertex 4


and also from vertex 5 to other vertices and so on.
Graphs …
 Strongly Connected
 A digraph is called strongly connected if there is a directed
path from any vertex to any other vertex.
1

Strongly Connected Graph


2

4 5

 Unilaterally Connected graph:


 For any pair of nodes (u,v), there is a path from either 'u' to 'v'
(or) 'v' to 'u'.
Graphs …
 In a digraph, the path is called a directed path and a cycle is
called as directed cycle.

1 4 6

 1,2 is a directed path


2 3 5 7
 1,3,5,7,6 is a directed path
 1,4,5 is not a directed path
 There is no directed cycle in the above graph.

 Simple Graph: is a graph with the following properties:


 No parallel edges.
 It can't have more than one loop at a given node.
Graphs …
 Degree
 There is no limitation of number of edges incident on one vertex.
 It could be none, one or more.
 The number of edges incident on a vertex determines its degree.
 The number of edges containing a node is called the degree of
that node.
1

2
3

7
5 6 4

The degree of vertex 3 is 4.


Graphs …
 In a digraph we attach an indegree and an outdegree to each of the
vetice.
 Indegree:
 The Indegree of a node 'u' is the number of edges ending at 'u' .
 Outdegree:
 The outdegree of a node 'u' is the number of edges starting from 'u'.
1
The indegree of vertex 5 is 2
The Outdegree of vertex 5 is 1
2

4 5
 Source:
 A node n is called source if outdegree(n) > 0 & indegree(n) = 0.
 Sink:
 A node n is called a sink if indegree(n) > 0 & outdegree(n) = 0.
Graphs …
 Tree Graph: It is a special type of graph.
 It is connected and
 There are no cycles in the graph.
1 2

7 5 6 3 4

9
A

8 9 10
3
B

6 7

D
E
8
Application of Graphs
 History:
 Maze-searching.
 Euler's crossing problem: the Konigsberg bridges

How to cross each bridge just once and return to


starting point?
Application of Graphs …
 Applications:
 Fundamental mathematical construct to represent
"connectivity".
 Appears in thousands of problems.
 Source of many classic problems: traveling salesman,
routing, spanning trees.
 Many "graph-structured" applications: networks,
transportation-systems, electronic circuits, molecules.
 Graphs are the essential data structure in network
applications.
 In solving the shortest path problems and longest path
problems.
Graph Representation
 The picture with circles (vertices) and lines (edges) is only a
depiction
=> a graph is purely a mathematical abstraction.
 Graph is a mathematical structure and finds its application in
many areas of interest in which problems need to be solved
using computers.
 Thus this mathematical structure must be represented as some
kind of data structures.
 Two such representations are commonly used. They are,
 Adjacent matrix and
 Adjacency list representation
 The choice of representation depends on the application and
function to be performed on the graph.
Graph Representation ...
Adjacency Matrix
 The adjacency matrix A for graph G = (V,E) with n vertices, is
an nXn matrix of bits, such that,
 Aij = 1 if there is an edge from vi to vj and
 Aij = 0 if there is no such edge

1
Vertice 1 2 3 4 5

1 0 1 1 0 1

2 1 0 1 0 0

2 5 3 1 1 0 1 1

4 0 0 1 0 1

5 1 0 1 1 0
3 4

 the adjacency matrix for an undirected graph


 is symmetric, as the lower and upper triangles are same.
 Also all the diagonal elements are zero.
Graph Representation ...
Adjacency Matrix

Vertex 1 2 3 4 5 6 7
1 4 6 1 0 1 1 1 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 1 0 0
4 0 0 0 0 0 1 0
5 0 0 0 1 0 0 1
6 0 0 0 0 0 0 0
2 3 5 7
7 0 0 0 0 0 1 0

 The total number of 1’s account for the number of edges in the
digraph.
 The number of 1’s in each row tells the out degree of the
corresponding vertex.
 The total number of 1's in each column tells the in degree of
the corresponding vertex
Graph Representation ...
Adjacency List Representation
 we store a graph as a linked structure.
 We store all the vertices in a list and then for each vertex, we
have a linked list of its adjacent vertices.
v1
V4

V6

V2 V3

V5

V1 V2 V3

V2 V3

V3 V4

V1 V5 V6
V4

V5 V4

V6
Graph Representation ...
Adjacency List Representation
Graph Traversal
 A graph traversal means visiting all the nodes of the graph
exactly once.
 Two graph traversal methods are commonly used. These are,
 Depth First Search (DFS)
 Breadth First Search (BFS)
Graph Traversal …
Depth First Search
 In graphs, we do not have any start vertex or any special
vertex signaled out to start traversal from.
 Therefore the traversal may start from any arbitrary vertex.
 We start with vertex v.
 An adjacent vertex is selected and a depth first search is initiated
from it.
 i.e. V1,V2,….Vk are adjacent vertices to vertex v.
 We may select any vertex from this list.
 Say we select v1.
 Now all the adjacent vertices to v1 are identified and all of those
are visited.
 Next v2 is selected and all its adjacent vertices visited and so on.
 This process continues till all the vertices are visited.
Graph Traversal …
Depth First Search …
 Let us start with V1.

V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V2, V8, and V3.
 Let us pick on V2.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V1, V4, V5.
 V1 is already visited.
 Let us pick on V4.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V2, V8.
 V2 is already visited.
 Let us pick on V8.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V4, V5, V1, V6, V7.
 V4 and V1 are already visited.
 Let us pick on V5.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V2, V8.
 Both are already visited.
 Therefore we back track.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 We have V6 and V7 unvisited in the list of V8.
 We may visit any. We visit V6.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent are V8 and V3.
 Obviously the choice is V3.
V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Depth First Search …
 Its adjacent vertices are V1, V7.
 We visit V7. V1

V2 V3

V4 V5 V6 V7

V8

 All the adjacent vertices of V7 are already visited, we backtrack and


find that we have visited all the vertices.
 Therefore the sequence of traversal is
V1, V2, V4, V8, V5, V6, V3, V7.
Graph Traversal …
Depth First Search …
 We may implement the depth first search method by using a
stack,
 pushing all unvisited vertices adjacent to the one just visited and
 popping the stack to find the next vertex to visit.
 Algorithm
 Iterative version:
 During the execution of these algorithms, each node will be in
one of the following states, called the status of node n.

 status=1(Ready state) :- Initial state of the node 'n'.


 status=2(waiting state):- 'n' is on queue or stack.
 Status = 3(Processed state):- Node 'n' has been processed.
Graph Traversal …
Depth First Search …
Step1: Initialize all nodes to the ready state i.e. status=1.
Step 2: Push the starting node A onto stack and change its status=2.
Step 3: Repeat while(stack !=empty)
a) Pop the top node 'n' of the stack. Process 'n' and change its
status to the processed state(i.e status=3)
b) Push onto stack all the neighbours of 'n' that are still in the
ready state and change their status to the waiting state(i.e.
status=2).
 Recursive Version:
dfs (vertex V)
{
visited [V] = true;
for each w adjacent to V
if (!visited [w])
dfs(w);
}
Graph Traversal …
Breadth First Search
 In DFS we pick on one of the adjacent vertices; visit all of the
adjacent vertices and back track to visit the unvisited adjacent
vertices.
 In BFS we first visit all the adjacent vertices of the start vertex
and then visit all the unvisited vertices adjacent to these and so
on. V1

V2 V3

V4 V5 V6 V7

V8
Graph Traversal …
Breadth First Search …
 We start with V1. Its adjacent vertices are V2, V8, V3. We visit
all one by one.
V1

V1

V2 V3

V8

 We pick on one of these, say V2. The unvisited adjacent


vertices to V2 are V4, V5. We visit both.
V1

V2 V8 V3

V4 V5
Graph Traversal …
Breadth First Search …
 We go back to the remaining unvisited adjacent vertices of V1
and pick on one of those say V3.
 The unvisited adjacent vertices are V6, V7.
V1

V2 V8 V3

V4 V5 V6 V7

 There are no more unvisited adjacent vertices of V8, V4, V5,


V6 and V7.
 Thus the sequence so generated is
V1 V2 V8 V3 V4 V5 V6 V7
Graph Traversal …
Breadth First Search …
 Here we need a queue instead of a stack to implement it.
 We add unvisited vertices adjacent to the one just visited, at the rear and
 read at front to find the next vertex to visit.
 Abstract view of BFS Algorithm:
Step1: Initialize all nodes to the ready state i.e. status=1.
Step 2: Put the starting node A onto queue and change its status=2.
Step 3: Repeat while(queue !=empty)
a) Remove the front node 'n' of the queue. Process 'n' and
change
its status to the processed state(i.e status=3)
b) Add to the rear of the queue all the neighbours of 'n' that are
still
in the ready state and change their status to the waiting
state(i.e.
status=2).
Graph Traversal …
Breadth First Search …
 Detailed view of BFS algorithm:
bfs (vertex v)
{
vertex w;
queue q;
visited [v] = true;
initialise (q);
addqueue (q,v)
while (! Emptyqueue(q))
{
deletequeue (q,v);
for all vertices w adjacent to v
if (!visited [w])
{
addqueue (q,w);
visited[w] = true;
}
}
Shortest Path Problem
Minimum Spanning tree
Thank you!!!

You might also like