0% found this document useful (0 votes)
11 views7 pages

DS_EXPERIMENT_15[3]

The document presents a program implementing graph traversals using Depth-First Search (DFS) in C and Breadth-First Search (BFS) in Java. It includes the structure for a graph, functions to create the adjacency matrix, perform DFS, and execute BFS from a given source vertex. The code demonstrates how to initialize the graph, add edges, and traverse the graph using both algorithms.

Uploaded by

Roshan dalvi
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)
11 views7 pages

DS_EXPERIMENT_15[3]

The document presents a program implementing graph traversals using Depth-First Search (DFS) in C and Breadth-First Search (BFS) in Java. It includes the structure for a graph, functions to create the adjacency matrix, perform DFS, and execute BFS from a given source vertex. The code demonstrates how to initialize the graph, add edges, and traverse the graph using both algorithms.

Uploaded by

Roshan dalvi
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/ 7

Experiment No.

15

Title : Write a program to Implement graph traversals by applying DFS & BFs

Name: Soham Anil Shende

DFS TRAVERSAL

// C code to implement DFS approach

#include <stdio.h>

#include <stdlib.h>

// Globally declared visited array

int vis[100];

// Graph structure to store number

// of vertices and edges and

// Adjacency matrix

struct Graph {

int V;

int E;

int** Adj;

};

// Function to input data of graph

struct Graph* adjMatrix()

struct Graph* G = (struct Graph*)

malloc(sizeof(struct Graph));

if (!G) {printf("Memory Error\n");

return NULL;

G->V = 7;

G->E = 7;

G->Adj = (int**)malloc((G->V) * sizeof(int*));


for (int k = 0; k < G->V; k++) {

G->Adj[k] = (int*)malloc((G->V) * sizeof(int));

for (int u = 0; u < G->V; u++) {

for (int v = 0; v < G->V; v++) {

G->Adj[u][v] = 0;

G->Adj[0][1] = G->Adj[1][0] = 1;

G->Adj[0][2] = G->Adj[2][0] = 1;

G->Adj[1][3] = G->Adj[3][1] = 1;

G->Adj[1][4] = G->Adj[4][1] = 1;

G->Adj[1][5] = G->Adj[5][1] = 1;

G->Adj[1][6] = G->Adj[6][1] = 1;

G->Adj[6][2] = G->Adj[2][6] = 1;

return G;

// DFS function to print DFS traversal of graph

void DFS(struct Graph* G, int u)

vis[u] = 1;

printf("%d ", u);

for (int v = 0; v < G->V; v++) {

if (!vis[v] && G->Adj[u][v]) {

DFS(G, v);

}}

// Function for DFS traversal

void DFStraversal(struct Graph* G)


{

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

vis[i] = 0;

for (int i = 0; i < G->V; i++) {

if (!vis[i]) {

DFS(G, i);

// Driver code

void main()

struct Graph* G;

G = adjMatrix();

DFStraversal(G);

}
BFS Traversal
// Java program to print BFS traversal from a given source vertex.

// BFS(int s) traverses vertices reachable from s.

Import java.io.*;

Import java.util.*;

// This class represents a directed graph using adjacency list

// representation

Class Graph

Private int V; // No. of vertices

Private LinkedList<Integer> adj[]; //Adjacency Lists

// Constructor

Graph(int v) {

V = v;

Adj = new LinkedList[v];

For (int i=0; i<v; ++i)

Adj[i] = new LinkedList();

// Function to add an edge into the graph

Void addEdge(int v,int w)

Adj[v].add(w);

// prints BFS traversal from a given source s


Void BFS(int s)

// Mark all the vertices as not visited(By default

// set as false)

Boolean visited[] = new boolean[V];

// Create a queue for BFS

LinkedList<Integer> queue = new LinkedList<Integer>();

// Mark the current node as visited and enqueue it

Visited[s]=true;

Queue.add(s);

While (queue.size() != 0) {

// Dequeue a vertex from queue and print it

S = queue.poll();

System.out.print(s+” “);

// Get all adjacent vertices of the dequeued vertex s

// If a adjacent has not been visited, then mark it

// visited and enqueue it

Iterator<Integer> I = adj[s].listIterator();

While (i.hasNext())

Int n = i.next();
If (!visited[n])

Visited[n] = true;

Queue.add(n);

// Driver method to

Public static void main(String args[])

Graph g = new Graph(4)

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(2, 0);
g.addEdge(2, 3);

g.addEdge(3, 3);

System.out.println(“Following is Breadth First Traversal “+

“(starting from vertex 2)”);

g.BFS(2); }}

You might also like