You are on page 1of 10

CSE2012 Design and Analysis of Algorithms

Lab-11
-R .Prithvi Jai
20BRS1235

1. Let 𝐺 = (𝑉, 𝐸) be a given graph. For any two vertices s and t in V(G),
write a C++ program to find a path in G that connects s to t and output all
the vertices on this path.

Code
#include <iostream>
#include <list>
using namespace std;

class Graph {
int V;
list<int>* adj;
void printAllPathsUtil(int, int, bool[], int[], int&);
public:
Graph(int V);
void addEdge(int u, int v);
void printAllPaths(int s, int d);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}

void Graph::printAllPaths(int s, int d)


{

bool* visited = new bool[V];


int* path = new int[V];
int path_index = 0;
for (int i = 0; i < V; i++)
visited[i] = false;
printAllPathsUtil(s, d, visited, path, path_index);
}
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int& path_index)
{

visited[u] = true;
path[path_index] = u;
path_index++;
if (u == d) {
for (int i = 0; i < path_index; i++)
cout << path[i] << " ";
cout << endl;
}
else
{

list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}

path_index--;
visited[u] = false;
}
int main()
{

Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 1);
g.addEdge(1, 3);
int s,d;
cout<<"Enter the Start:"<<endl;
cin>>s;
cout<<"Enter the End:"<<endl;
cin>>d;
cout << "Following are all different paths from " << s << " to " << d << endl;
g.printAllPaths(s, d);
return 0;
}

Output
2. Let 𝐺 = (𝑉, 𝐸) be a given graph with a non-negative weight function 𝑤:
𝐸(𝐺) → ℤ + associated with each edge 𝑒 ∈ 𝐸(𝐺). Let s and t be two
vertices and k be a positive integer that are given as input. Write a program
to find and output a single weighted path P from s to t such that the total
weight of the path P is at least k. If no such path exists, the output No. If a
path exists, then output all the vertices on the path including s and t and
also the weight of the path P.

Code

#include <iostream>
#include <list>
using namespace std;
int w[4][4]={{0,4,5,3},{4,0,0,2},{5,0,0,0},{3,2,0,0}};
int K=9; // the least value the cost should be
class Graph {
int V;
list<int>* adj;
void printAllPathsUtil(int, int, bool[], int[], int&);
public:
Graph(int V);
void addEdge(int u, int v);
void printAllPaths(int s, int d);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}

void Graph::printAllPaths(int s, int d)


{
bool* visited = new bool[V];
int* path = new int[V];
int path_index = 0;
for (int i = 0; i < V; i++)
visited[i] = false;
printAllPathsUtil(s, d, visited, path, path_index);
}
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int& path_index)
{

visited[u] = true;
path[path_index] = u;
path_index++;
if (u == d) {
int cost=0;
for (int i = 0; i < path_index; i++)
{
int z=path[i];
int y=path[i+1];
cost=cost+w[z][y];
//cout << path[i] << " ";
}
if(cost>=K)
{
for (int i = 0; i < path_index; i++)
{
cout << path[i] << " ";

}
cout << endl;
}
else
{

list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}

path_index--;
visited[u] = false;
}
int main()
{

Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 1);
g.addEdge(1, 3);

int s,d;
cout<<"Enter the Start:"<<endl;
cin>>s;
cout<<"Enter the End:"<<endl;
cin>>d;
cout << "Following are all different paths from " << s << " to " << d << " cost greater than
"<<K<< endl;
g.printAllPaths(s, d);
return 0;
}

Output
3. For a graph G ,start node s and end node d find the minimum cost
between source s and destination d.

Code

#include<iostream>
#include<climits>
using namespace std;

int miniDist(int distance[], bool Tset[])


{
int minimum=INT_MAX,ind;

for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}

void DijkstraAlgo(int graph[6][6],int src,int d)


{
int distance[6];
bool Tset[6];

for(int k = 0; k<6; k++)


{
distance[k] = INT_MAX;
Tset[k] = false;
}

distance[src] = 0;
for(int k = 0; k<6; k++)
{
int m=miniDist(distance,Tset);
Tset[m]=true;
for(int k = 0; k<6; k++)
{

if(!Tset[k] && graph[m][k] && distance[m]!=INT_MAX &&


distance[m]+graph[m][k]<distance[k])
distance[k]=distance[m]+graph[m][k];
}
}
cout<<"from "s<<"to "<<d<<endl;
cout<<distance[d]<<endl;
}

int main()
{
int graph[6][6]={
{0, 7, 2, 0, 0, 0},
{7, 0, 3, 0, 2, 0},
{2, 3, 0, 4, 1, 0},
{0, 0, 4, 0, 3, 3},
{0, 2, 1, 3, 0, 5},
{0, 0, 0, 3, 5, 0}};
int s,d;
cout<<"Enter the Start:"<<endl;
cin>>s;
cout<<"Enter the End:"<<endl;
cin>>d;
DijkstraAlgo(graph,s,d);
return 0;
}

Output

You might also like