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

Exp 10 Harmeet

Uploaded by

amitybhavya
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)
20 views5 pages

Exp 10 Harmeet

Uploaded by

amitybhavya
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 – 10 (A)

Aim: From a given starting node in a digraph, print all the nodes reachable by using BFS/DFS
method.

Theory:
Graph Traversal is a technique used to systematically visit all the nodes in a graph. Two widely
used algorithms for this are Breadth-First Search (BFS) and Depth-First Search (DFS).
Although both aim to explore nodes and edges, they follow different approaches:

• Breadth-First Search (BFS): BFS explores the graph level by level, visiting all
neighboring nodes at the current depth before moving on to the next level. It typically
employs a queue to track the next node to visit.
• Depth-First Search (DFS): DFS explores the graph by going as deep as possible along
each path before backtracking. It continues down a branch until it finds a node with no
unvisited neighbors. DFS can be implemented recursively (using the call stack) or with
an explicit stack.

Pseudo Code:
BFS
FUNCTION BFS(graph, start):
INITIALIZE an empty queue
INITIALIZE a set for visited nodes
ENQUEUE start node into the queue
MARK start node as visited

WHILE the queue is not empty DO:


node = DEQUEUE the front node from the queue
PRINT node

FOR each neighbor of node DO:


IF neighbor is not in visited THEN:
ENQUEUE neighbor into the queue
MARK neighbor as visited
END WHILE
END FUNCTION

DFS
FUNCTION DFS(graph, node, visited):
IF node is not in visited THEN:
PRINT node
MARK node as visited

FOR each neighbor of node DO:


CALL DFS(graph, neighbor, visited)
END FUNCTION

Code:
#include <iostream>
#include <queue>
using namespace std;

const int MAX_NODES = 6;

void BFS(int graph[MAX_NODES][MAX_NODES], int start, int n) {


bool visited[MAX_NODES] = {false};
queue<int> q;

q.push(start);
visited[start] = true;

cout << "BFS from node " << start << ": ";

while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";

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


if (graph[node][i] == 1 && !visited[i]) {
q.push(i);
visited[i] = true;
}
}
}
cout << endl;
}

void DFSUtil(int graph[MAX_NODES][MAX_NODES], int node, bool visited[], int n) {


visited[node] = true;
cout << node << " ";

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


if (graph[node][i] == 1 && !visited[i]) {
DFSUtil(graph, i, visited, n);
}
}
}

void DFS(int graph[MAX_NODES][MAX_NODES], int start, int n) {


bool visited[MAX_NODES] = {false};
cout << "DFS from node " << start << ": ";
DFSUtil(graph, start, visited, n);
cout << endl;
}

int main() {
int graph[MAX_NODES][MAX_NODES] = {
{0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};

int startNode = 0;

BFS(graph, startNode, MAX_NODES);


DFS(graph, startNode, MAX_NODES);

return 0;
}

Output

Time complexities analysis:


1. BFS (Breadth-First Search)
Time Complexity: O(V + E), where: V = number of vertices (nodes) E = number of edges
Explanation: BFS explores every node once (O(V)) and every edge once (O(E)).

2. DFS (Depth-First Search)


Time Complexity: O(V + E), where: V = number of vertices E = number of edges
Explanation: DFS similarly explores all vertices and edges, visiting each vertex and edge
once.
Experiment – 10 (B)

Aim: Implement Traveling Salesman problem based on Branch and Bound technique.

Theory:
The Traveling Salesman Problem (TSP) is a well-known optimization challenge where a
salesman must visit a collection of cities exactly once and return to the starting point while
minimizing the overall travel distance or cost. Since TSP is NP-hard, no polynomial-time
solution is currently known. The Branch and Bound method offers a systematic approach to
solving TSP by breaking the problem down into smaller subproblems (branches) and
discarding those that cannot lead to a better solution than the best one found so far (bounding).

Pseudo code:
function TSP(graph, n):
Initialize min_cost = infinity
Initialize visited array of size n
Call branch_and_bound(graph, visited, 0, 0, 1, n, min_cost)
return min_cost

function branch_and_bound(graph, visited, curr_pos, cost, level, n, min_cost):


if level == n and graph[curr_pos][0] > 0:
min_cost = min(min_cost, cost + graph[curr_pos][0])
return

for i from 0 to n:
if visited[i] == false and graph[curr_pos][i] > 0:
visited[i] = true
branch_and_bound(graph, visited, i, cost + graph[curr_pos][i], level + 1, n, min_cost)
visited[i] = false

Code
#include <iostream>
#include <limits.h>

using namespace std;

const int MAX_NODES = 10;

int minCost = INT_MAX;

void branchAndBound(int graph[MAX_NODES][MAX_NODES], bool visited[], int


currPos, int cost, int level, int n) {
if (level == n && graph[currPos][0] > 0) {
minCost = min(minCost, cost + graph[currPos][0]);
return;
}

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


if (!visited[i] && graph[currPos][i] > 0) {
visited[i] = true;
branchAndBound(graph, visited, i, cost + graph[currPos][i], level + 1, n);
visited[i] = false;
}
}
}

int TSP(int graph[MAX_NODES][MAX_NODES], int n) {


bool visited[MAX_NODES] = {false};
visited[0] = true;
branchAndBound(graph, visited, 0, 0, 1, n);
return minCost;
}

int main() {
int graph[MAX_NODES][MAX_NODES] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
int n = 4;
cout << "Minimum cost of visiting all cities: " << TSP(graph, n) << endl;
return 0;
}

Output

Time complexity analysis


Worst-Case Time Complexity: O(n!) where n = number of cities (nodes in the graph)
Explanation:
In the worst case, Branch and Bound examines all possible city permutations, which has a
factorial time complexity (O(n!)). However, by pruning based on bounds, the number of
paths explored can be reduced in practice. The best-case performance depends on how early
pruning happens, potentially boosting efficiency, though the worst-case scenario still
remains O(n!).

You might also like