0% found this document useful (0 votes)
4 views5 pages

Experiment 9

The document outlines an experiment to implement Depth First Search (DFS) using a linked list representation of a graph. It details the theory behind DFS, the algorithm steps for implementation, and provides a complete C program for creating a graph, adding edges, and performing DFS traversal. The time complexity is O(V + E) and auxiliary space complexity is O(V + E), where V is the number of vertices and E is the number of edges.

Uploaded by

Kartikeya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Experiment 9

The document outlines an experiment to implement Depth First Search (DFS) using a linked list representation of a graph. It details the theory behind DFS, the algorithm steps for implementation, and provides a complete C program for creating a graph, adding edges, and performing DFS traversal. The time complexity is O(V + E) and auxiliary space complexity is O(V + E), where V is the number of vertices and E is the number of edges.

Uploaded by

Kartikeya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

9
Objective: To implement DFS using linked list.

Theory: Depth First Traversal (or DFS) for a graph is similar to Depth First
Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain
cycles (a node may be visited twice). To avoid processing a node more than
once, use a boolean visited array. A graph can have more than one DFS traversal.

The step by step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that
vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of
the stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex
on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.

Algorithm:
Initialization:
1.1. Define structures:
 AdjListNode with fields dest and next.
 AdjList with a field head.
 Graph with fields V and array.
1.2. Create utility functions:
 newAdjListNode(int dest) to create a new adjacency list node.
 createGraph(int V) to initialize a graph with V vertices and empty
adjacency lists.
Add Edges:
 addEdge(struct Graph* graph, int src, int dest):
o Create a new node with the destination vertex.
o Insert the new node at the beginning of the adjacency list of
the source vertex.
DFS Traversal:
 DFSUtil(int v, int visited[], struct Graph* graph):
o Mark v as visited.
o Print v.
o For each adjacent vertex u of v, if u is not visited, recursively
call DFSUtil(u, visited, graph).
 DFS(struct Graph* graph, int v):
o Allocate and initialize the visited array to false (0).
o Call DFSUtil(v, visited, graph).

Program:
#include <stdio.h>
#include <stdlib.h>

// A structure to represent an adjacency list node


struct AdjListNode {
int dest;
struct AdjListNode* next;
};

// A structure to represent an adjacency list


struct AdjList {
struct AdjListNode* head;
};

// A structure to represent a graph. A graph is an array of adjacency lists.


// Size of array will be V (number of vertices in graph)
struct Graph {
int V;
struct AdjList* array;
};

// A utility function to create a new adjacency list node


struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct
AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

// A utility function that creates a graph of V vertices


struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists. Size of array will be V


graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

// Initialize each adjacency list as empty by making head as NULL


for (int i = 0; i < V; ++i)
graph->array[i].head = NULL;

return graph;
}

// Adds an edge to an undirected graph


void addEdge(struct Graph* graph, int src, int dest) {
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the beginning
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}

// A utility function to print DFS traversal of the vertices reachable from v


void DFSUtil(int v, int visited[], struct Graph* graph) {
// Mark the current node as visited and print it
visited[v] = 1;
printf("%d ", v);

// Recur for all the vertices adjacent to this vertex


struct AdjListNode* adjList = graph->array[v].head;
while (adjList != NULL) {
int connectedVertex = adjList->dest;
if (!visited[connectedVertex]) {
DFSUtil(connectedVertex, visited, graph);
}
adjList = adjList->next;
}
}

// The function to do DFS traversal. It uses recursive DFSUtil()


void DFS(struct Graph* graph, int v) {
// Mark all the vertices as not visited
int* visited = (int*) malloc(graph->V * sizeof(int));
for (int i = 0; i < graph->V; i++)
visited[i] = 0;

// Call the recursive helper function to print DFS traversal


DFSUtil(v, visited, graph);

// Free the allocated memory


free(visited);
}

// Driver code
int main() {
// Create a graph given in the above diagram
int V = 4;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 0);
addEdge(graph, 2, 3);
addEdge(graph, 3, 3);

printf("Following is Depth First Traversal (starting from vertex 2)\n");


DFS(graph, 2);

return 0;
}

Output:
Following is Depth First Traversal (starting from vertex 2)
2301

The complexity of the above method:

Time Complexity: O(V + E) Auxiliary Space: O(V + E)

where V is the number of vertices and E is the number of edges.

You might also like