Data Stracture & Algorithm Chapter Eight Graph
Data Stracture & Algorithm Chapter Eight Graph
GRAPHS
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
2 5
3 2
7
Ex: 5 6 4
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
2
A weakly connected graph
4 5
4 5
1 4 6
2
3
7
5 6 4
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
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
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
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
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