The document discusses graph theory concepts including representations of graphs like adjacency matrix and adjacency list, and graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). It provides pseudocode for DFS and BFS algorithms and examples of their use. It also notes that graph theory is used for shortest path algorithms like Dijkstra's, Prim's and Kruskal's.
1. Prof. Sandeep Vishwakarma
Chandrabhan sharma college Powai
FYBCA Sem-II
• Introduction
Terminologies: graph, node (Vertices), arcs (edge), directed graph, in -
degree, out -degree, adjacent, successor, predecessor, relation,
weight, path, length.
• Representations of a graph
a. Array Representation
b. Linked list Representation
• Traversal of graphs
a. Depth-first search (DFS).
b. Breadth-first search (BFS).
• Applications of Graph
Data structure using C ++ (CMP505)
Unit-7
Graph
8. Graph data structure is represented using
following representations...
Adjacency Matrix
Incidence Matrix
Adjacency List
Data Structure and Algorithm
9. In this representation, the graph is represented
using a matrix of size total number of vertices
by a total number of vertices. That means a
graph with 4 vertices is represented using a
matrix of size 4X4. In this matrix, both rows
and columns represent vertices. This matrix is
filled with either 1 or 0. Here, 1 represents that
there is an edge from row vertex to column
vertex and 0 represents that there is no edge
from row vertex to column vertex.
Data Structure and Algorithm
10. Data Structure and Algorithm
Directed Graph Representation
Undirected Graph Representation
11. In this representation, the graph is represented using a
matrix of size total number of vertices by a total
number of edges. That means graph with 4 vertices and
6 edges is represented using a matrix of size 4X6. In
this matrix, rows represent vertices and columns
represents edges. This matrix is filled with 0 or 1 or -1.
Here, 0 represents that the row edge is not connected to
column vertex, 1 represents that the row edge is
connected as the outgoing edge to column vertex and -1
represents that the row edge is connected as the
incoming edge to column vertex.
14. Graph traversal is a technique used for a searching
vertex in a graph. The graph traversal is also used to
decide the order of vertices is visited in the search
process. A graph traversal finds the edges to be used in
the search process without creating loops. That means
using graph traversal we visit all the vertices of the
graph without getting into looping path.
There are two graph traversal techniques and they are as
follows...
DFS (Depth First Search)
BFS (Breadth First Search)
15. Data Structure and Algorithm
The basic idea behind this algorithm is that it
traverses the graph using recursion
◦ Go as far as possible until you reach a deadend
◦ Backtrack to the previous path and try the next
branch
◦ The graph below, started at node a, would be
visited in the following order: a, b, c, g, h, i, e, d,
f, j
d
a b
h i
c
g
e
j
f
16. Vertices initially colored white
Then colored gray when
discovered
Then black when finished
Data Structure and Algorithm
17. Discover time d[u]: when u is
first discovered
Finish time f[u]: when backtrack
from u
d[u] < f[u]
Data Structure and Algorithm
23. Data Structure and Algorithm
1 | | |
|
5 |
3 | 4
2 | |
source
vertex
d f
24. Data Structure and Algorithm
1 | | |
|
5 | 6
3 | 4
2 | |
source
vertex
d f
25. Data Structure and Algorithm
1 | | |
|
5 | 6
3 | 4
2 | 7 |
source
vertex
d f
26. Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 |
source
vertex
d f
27. Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 9 |
source
vertex
d f
28. Data Structure and Algorithm
1 | 8 | |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
29. Data Structure and Algorithm
1 | 8 |11 |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
30. Data Structure and Algorithm
1 |12 8 |11 |
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
31. Data Structure and Algorithm
1 |12 8 |11 13|
|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
32. Data Structure and Algorithm
1 |12 8 |11 13|
14|
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
33. Data Structure and Algorithm
1 |12 8 |11 13|
14|15
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
34. Data Structure and Algorithm
1 |12 8 |11 13|16
14|15
5 | 6
3 | 4
2 | 7 9 |10
source
vertex
d f
35. Data Structure and Algorithm
DFS(G)
for each vertex u in V,
color[u]=white; p[u]=NIL
time=0;
for each vertex u in V
if (color[u]=white)
DFS-VISIT(u)
36. Data Structure and Algorithm
DFS-VISIT(u)
color[u]=gray;
time = time + 1;
d[u] = time;
for each v in Adj(u) do
if (color[v] = white)
p[v] = u;
DFS-VISIT(v);
color[u] = black;
time = time + 1; f[u]= time;
DFS: Algorithm (Cont.)
source
vertex
38. Search for all vertices that are directly
reachable from the root (called level 1
vertices)
After mark all these vertices, visit all vertices
that are directly reachable from any level 1
vertices (called level 2 vertices), and so on.
In general, level k vertices are directly
reachable from a level k – 1 vertices
Data Structure and Algorithm
39. White vertices have not been discovered
◦ All vertices start out white
Grey vertices are discovered but not fully explored
◦ They may be adjacent to white vertices
Black vertices are discovered and fully explored
◦ They are adjacent only to black and gray vertices
Explore vertices by scanning adjacency list of grey
vertices
Data Structure and Algorithm
40. BFS(G, s)
For each vertex u in V – {s},
color[u] = white;
d[u] = infinity;
p[u] = NIL
color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = empty queue
ENQUEUE(Q,s)
while (Q not empty)
u = DEQUEUE(Q)
for each v Adj[u]
if color[v] = WHITE
then color[v] = GREY
d[v] = d[u] + 1; p[v] = u
ENQUEUE(Q, v);
color[u] = BLACK;
51. In computer science graph theory is used for
the study of algorithms like:
1. Dijkstra's Algorithm
2. Prims's Algorithm
3. Kruskal's Algorithm
Note: Above algorithms are used to find
shortest path between two vertices.