You are on page 1of 1

Algorithm for Operations on Graph:-

I. Breadth-First Search:-
array 'visited' to track status of vertices.
2-D array 'graph' for graph adjacency matrix.
queue 'queue' to store vertices.

1. Select any vertex from the graph(v).


2. Enqueue it in queue.
3. Repeat step 4,5,6 while queue is not empty.
4. Dequeue queue and set element as v.
5. Enqueue all it's unqueued,unvisited,adjacent vertices.
6. Set visited[v]=True.
7. Exit.

II. Depth-First Search:-


array 'visited' to track status of vertices.
2-D array 'graph' for graph adjacency matrix.
stack 'st' to store vertices.

1. Select any vertex from the graph(v).


2. Print v and make visited[v]=True,
3. Repeat step 4,5 while all the vertices are visited.
4. 'i' Push all adjacent unvisited vertices in 'st'.
'ii' Else go to step 5.
5. Pop last vertex and set it to v and print v.
6.Exit.

III. Minimum Cost Spanning-Tree:-


array 'visited' to track status of vertices.
2-D array 'graph' for graph adjacency matrix.
set to store edges min cost(set to inf).

1. Select any vertex from the graph(v).


2. Repeat step 3,4 while all vertices are not connected.
3. Set visited[v]=True.
4. Select mininum cost edge from v which is not connected.
5. Print v and set v to edge another node.
6. Exit.

IV. Shortest Path (Dijkstra Algorithm):-


array 'sptSet' to track status of vertices.
2-D array 'graph' for graph adjacency matrix.
distance to store edges min cost(set to inf).
source is reference node of graph.

1. Set distance[source]=0.
2. Set min distance of all other vertices from source.
3. while no_Vetices-1 repeat step 4.
4. Set min distance between source and nodes
by keeping a vertex as intermediate node.
5. Print all min_distance between source and vertices.
6. Exit.

You might also like