0% found this document useful (0 votes)
13 views9 pages

Ds 10

The document outlines Experiment No. 10 focused on implementing Depth First Search (DFS) for graph traversal. It includes the aim, objectives, theoretical background, algorithm, and code for executing DFS using a stack data structure. The experiment emphasizes understanding graph representation and traversal techniques.
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)
13 views9 pages

Ds 10

The document outlines Experiment No. 10 focused on implementing Depth First Search (DFS) for graph traversal. It includes the aim, objectives, theoretical background, algorithm, and code for executing DFS using a stack data structure. The experiment emphasizes understanding graph representation and traversal techniques.
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/ 9

Experiment No.

10
Implementation of Graph traversal techniques - Depth First
Search
Date of Performance:23/09/24
Date of Submission:30/09/10
Experiment No. 10: Depth First Search and Breath First Search
Aim : Implementation of DFS traversal of graph.
Objective:
1. Understand the Graph data structure and its basic operations.
2. Understand the method of representing a graph.
3. Understand the method of constructing the Graph ADT and defining its operations

Theory:
A graph is a collection of nodes or vertex, connected in pairs by lines referred as edges. A
graph can be directed or undirected graph.
One method of traversing through nodes is depth first search. Here we traverse from
starting node and proceeds from top to bottom. At a moment we reach a dead end from where the
further movement is not possible and we backtrack and then proceed according to left right order.
A stack is used to keep track of a visited node which helps in backtracking.

DFS Traversal –0 1 2 3 4

Algorithm
Algorithm: DFS_LL(V)
Input: V is a starting vertex
Output : A list VISIT giving order of visited vertices during traversal.
Description: linked structure of graph with gptr as pointer
1. if gptr = NULL then
print “Graph is empty” exit
2. u=v
3. OPEN.PUSH(u)
4. while OPEN.TOP !=NULL do
u=OPEN.POP()
if search(VISIT,u) = FALSE then
INSERT_END(VISIT,u)
Ptr = gptr(u)
While ptr.LINK != NULL do
Vptr = ptr.LINK
OPEN.PUSH(vptr.LABEL)
End while
End if
End while
5. Return VISIT

6. Stop
Code:

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

typedef struct GraphNode {

int label;

struct GraphNode* next;

} GraphNode;

typedef struct Graph {

int numVertices;

GraphNode** adjLists;

} Graph;

typedef struct Stack {

int* items;

int top;

int maxSize;

} Stack;

Stack* createStack(int size) {

Stack* stack = (Stack*)malloc(sizeof(Stack));

stack->maxSize = size;

stack->top = -1;

stack->items = (int*)malloc(sizeof(int) * size);

return stack;
}

bool isEmpty(Stack* stack) {

return stack->top == -1;

void push(Stack* stack, int item) {

stack->items[++stack->top] = item;

int pop(Stack* stack) {

return stack->items[stack->top--];

Graph* createGraph(int vertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = vertices;

graph->adjLists = (GraphNode**)malloc(vertices * sizeof(GraphNode*));

for (int i = 0; i < vertices; i++) {

graph->adjLists[i] = NULL;

return graph;

void addEdge(Graph* graph, int src, int dest) {


GraphNode* newNode = (GraphNode*)malloc(sizeof(GraphNode));

newNode->label = dest;

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

bool search(int* visited, int size, int vertex) {

for (int i = 0; i < size; i++) {

if (visited[i] == vertex) {

return true;

return false;

void DFS(Graph* graph, int startVertex) {

int* visited = (int*)malloc(graph->numVertices * sizeof(int));

int visitCount = 0;

Stack* openStack = createStack(graph->numVertices);

push(openStack, startVertex);

while (!isEmpty(openStack)) {

int currentVertex = pop(openStack);


if (!search(visited, visitCount, currentVertex)) {

visited[visitCount++] = currentVertex;

printf("%d ", currentVertex);

GraphNode* adjList = graph->adjLists[currentVertex];

while (adjList != NULL) {

push(openStack, adjList->label);

adjList = adjList->next;

free(visited);

free(openStack->items);

free(openStack);

int main() {

int vertices, edges, src, dest;

printf("Enter the number of vertices: ");

scanf("%d", &vertices);

printf("Enter the number of edges: ");

scanf("%d", &edges);

Graph* graph = createGraph(vertices);


for (int i = 0; i < edges; i++) {

printf("Enter edge %d (format: src dest): ", i + 1);

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

int startVertex;

printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS starting from vertex %d:\n", startVertex);

DFS(graph, startVertex);

printf("\n");

for (int i = 0; i < graph->numVertices; i++) {

GraphNode* temp = graph->adjLists[i];

while (temp != NULL) {

GraphNode* toFree = temp;

temp = temp->next;

free(toFree);

free(graph->adjLists);

free(graph);

return 0;

}
Output:

Conclusion:

You might also like