The document discusses graphs and graph algorithms. It defines what a graph is - a non-linear data structure consisting of vertices connected by edges. It describes different graph representations like adjacency matrix, incidence matrix and adjacency list. It also explains graph traversal algorithms like depth-first search and breadth-first search. Finally, it covers minimum spanning tree algorithms like Kruskal's and Prim's algorithms for finding minimum cost spanning trees in graphs.
2. Submitted To :
Dr. Pooja Shrimali
Submitted By :
Komal Paliwal
& Jitendra Bhatia
3.
4.
5. GRAPH is 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 connect
the vertices. A graph is defined as follows....
“Graph is a collection of nodes and edges which
connects vertices 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.
6. The following is a graph with 5 vertices and 6
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),(C,D),(B,E),(E,D)}.
Edges
Vertices
A B
C D
E
7. We use the following term in data
structure...........
Vertex
Edges
a. Undirected graph
b. Directed graph
c. Weighted graph
Undirected graph
Directed graph
Mixed graph
End vertices or End points
Origin
Description of these
terminology’s.
Destination
Adjacent
Incident
Outgoing Edge
Incoming Edge
Degree
In Degree
Out degree
Parallel edges or Multiple
edges
Self loop
Simple graph
Path
8. Graph data structure is represented
using following representations.....
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
9. 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 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 o represents there is no edge from
vertex to column vertex.
11. 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, o 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.
12. E1 E2 E3 E4 E5 E6 E7 E8
A
B
C
D
E
1
-1
0
0
0
1
0
-1
0
0
-1
0
0
1
0
0
1
0
-1
0
0
0
1
-1
0
0
0
0
1
-1
0
1
0
0
-1
0
0
0
1
0
A
DC
B
E
E1
E3
E7
E6
E4E2
E5
E8
For example :-
13. In this representation, every vertex of
graph contains list of its adjacent vertices.
For example :-This representation can also
be implemented using array as follows...
Reference Array
Adjacency Array
E
A B
C D
0
1
2
3
4
B C
D E
D
A D E
14. For example, consider the following directed graph representation
implemented using linked list....
E
BA
DC
A
B
C
D
E
E
E
D
B
D
D
A
C
15. Graph traversals is technique used
for searching a vertex in a graph. The graph
traversal is also used to decide the order of
vertices to be visit in the search process. A
graph traversal is also edges to be used in
the search process without creating loops
that means using graph traversal we visit all
vertices of graph without getting into looping
path.
16. There are two traversal techniques and
they are as follows…
1. Depth First Traversal (D F S)
2. Breadth First Traversal (B F S)
17. DFS traversal of a graph, produces a
spanning tree as final result. Spanning Tree is
a graph without any loops. We use Stack data
structure with maximum size of total number
of vertices in the graph to implement DFS
traversal of a graph.
18. Step 1: Define a Stack of size total number of vertices in the
graph.
Step 2: Select any vertex as starting point for traversal. Visit
that vertex and push it on the Stack.
Step 3: Visit any one of the adjacent vertex of the vertex
which is at top of the stack which is not visited and push it
on to the stack.
Step 4: Repeat step 3 until there are no new vertex to be visit
from the vertex on top of the stack.
Step 5: When there is no new vertex to be visit then use
tracking and pop one vertex from the stack.
Step 6: Repeat steps 3,4and5 until stack becomes Empty.
Step 7: When stack becomes Empty, then produce final
spanning tree by removing unused edges from the graph
Back tracking is coming back to the vertex from which we
came to current vertex.
19. Consider the following example graph to
perform DFS traversal :
DFS traversal on this example:
A B
DC
E
20. 1. For an unweighted graph
2. Detecting cycle in a graph
3. Path finding
4. Topological sorting
5. Finding strongly connected component of a
graph
6. Solving puzzles with only one solution(such
as mazes).
21. For an unweighted graph, DFS traversal of the
graph produces the minimum spanning tree and all
pair shortest path tree.
Detecting cycle in a graph, A graph has cycle if and
if we see a back edge during DFS. So we can run
DFS for the graph and check for back edges.
Path finding, We can specialize the DFS algorithm to
find a path between two given vertices u and z.
Call DFS(G,u)with u as the start vertex.
Use a stack S to keep track of the path between the
start vertex and the current vertex.
As soon as destination vertex z is encountered,
return the path as the contents of the stack
22. DFS-iterative(G,s):
//Where G is graph and s is source vertex.
let S be stack
s.push(s)
marks s as visited
while(S is not empty);
//Pop a vertex from stack to visit next
v=S.top( )
S.pop( )
//Push all the neighbours of v in stack that
are not visited for all neighbors w of v in
Graph G:
if w is not visited :
s.push(w)
mark w as visited
23. The time complexity of DFS is O(V+E),
Where V is the number of nodes and
E is the number of edges.
Program of Depth First Traversal :
24. Breadth first search(BFS) algorithm traverses
a graph in a breadth ward motion and uses a
queue to remember to get the next vertex to
start a search, when a dead end occurs in any
iteration.
It employs the following rules :
Rule1 :Visit the adjacent vertex. Mark it as visited.
Display it. Insert it in a queue.
Rule2 :If no adjacent vertex is found , remove the
vertex from the queue.
Rule3 :Repeat Rule1 and Rule 2 till the queue is
empty.
25. Consider the following example graph to
perform BFS traversal :
BFS traversal on this example :
S
BA C
D
26. BFS(G,s) //where G is a graph and s is the source node
let Q be queue.
Q.enqueue(s) //Inserting s in queue until its neighbour
vertices are marked.
marks s as visited.
while(Q is not empty)
//Removing that vertex from queue,
whose neighbour will be visited now
v=Q.dequeue( )
//processing all the neighbours of v
for all neighbours w of v in Graph G
if w is not visited
Q.enqueue(w)
// Stores w in Q to further visit its neighbour
mark w as visited.
27. The time complexity of BFS is O(V+E),
Where V is the number of nodes and
E is the number of edges.
program on BFS graph traversal :
28. An undirected and connected graph
G=(V,E), a spanning tree of the graph G is a
tree that spans G (that is, it includes every
vertex of G) and is a sub-graph of G (every
edge in the tree belongs to G).
29. The cost of the spanning tree is the sum of the
weights of all the edges in the tree. There can be
many spanning trees. Minimum spanning tree is
the spanning tree where the cost is minimum
among all the spanning trees. There also can be
many minimum spanning trees.
Minimum spanning tree has direct application
in the design of networks. It is used in algorithms
approximating the travelling salesman problem,
multi-terminal minimum cut problem and
minimum-cost weighted perfect matching.
31. There are two famous algorithms for
finding the Minimum Spanning Tree (MST ) :
1) Kruskal’s Algorithm
2) Prim’s Algorithm
32. Kruskal’s Algorithm builds the spanning
tree by adding edges one by one into a
growing spanning tree. Kruskal's algorithm
follows greedy approach as in each iteration
it finds an edge which has least weight and
add it to the growing spanning tree.
33. Algorithm Steps:
Sort the graph edges with respect to their
weights.
Start adding edges to the MST from the edge
with the smallest weight until the edge of the
largest weight.
Only add edges which doesn't form a cycle ,
edges which connect only disconnected
components.
34. So now the question is how to check
if 2 vertices are connected or not ?
This could be done using DFS which starts
from the first vertex, then check if the second
vertex is visited or not. But DFS will make time
complexity large as it has an order
of O(V+E) where V is the number of vertices, E is
the number of edges. So the best solution
is "Disjoint Sets":
Disjoint sets are sets whose intersection is the
empty set so it means that they don't have any
element in common.
36. Prim’s Algorithm also use Greedy
approach to find the minimum spanning tree.
In Prim’s Algorithm we grow the spanning
tree from a starting position. Unlike
an edge in Kruskal's, we add vertex to the
growing spanning tree in Prim's.
37. Algorithm Steps:
Maintain two disjoint sets of vertices. One containing
vertices that are in the growing spanning tree and
other that are not in the growing spanning tree.
Select the cheapest vertex that is connected to the
growing spanning tree and is not in the growing
spanning tree and add it into the growing spanning
tree. This can be done using Priority Queues. Insert
the vertices, that are connected to growing spanning
tree, into the Priority Queue.
Check for cycles. To do that, mark the nodes which
have been already selected and insert only those
nodes in the Priority Queue that are not marked.
39. Time Complexity of kruskal’s algorithm:
In Kruskal’s algorithm, most time consuming
operation is sorting because the total complexity
of the Disjoint-Set operations will be O(E logV),
which is the overall Time Complexity of the
algorithm.
Time Complexity of prim’s algorithm:
The time complexity of the Prim’s Algorithm
is O((V+E) logV) because each vertex is inserted
in the priority queue only once and insertion in
priority queue take logarithmic time.
40. A graph is a non-linear data structure, which
consist of vertices (or nodes) connected by
edges (or arcs)where edges may be directed
or undirected.
In computer science graphs are used to
represent the flow of computation.
Thus the development of algorithm to handle
graphs is of major interest in the field of
computer science.
41. 1. Social network graphs :-An example twitter
and face-book graph of who follows whom .
this can be use determine how information
flows.
2. Transportation network :-In road network
and public transportation vertices are stops
and edges are the links between them.
3. Utility graphs :-The power grid , the internet
and the water network are all example of
graphs where vertices connection points and
edges the wires or pipes between them.
42. 4. Document link graph :-The best known
example is the link graph of the web where
each web page is a vertex and each
hyperlink a directed edge.
5. Protein –protein graph :-Vertices represent
proteins and edges represent interaction
between them that carry our some biological
function in the cell.
6. Network packet traffic graphs :-Vertices are
IP(internet protocol) address an a edges are
the packets that flow between them.
43. 7. Scene graph :- In graphics and computer
games scene graphs represent the logical or
special relationships between objects in a
scene .such graph very important in the
computer game industry.
8. Semantic network :-Vertices represents
words or concepts and edges represent the
relationship among the words or concepts.