Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ASSIGNMENT-2 BCS

NAME-PAVAN KUMAR M PUJARI

ROLL NO-056

1.
#include <iostream>
#include <list>
#include <string>
#define max 4
using namespace std;
string strings1[max],strings2[max]; // define max string

class Graph {
int V;
list<int>* adj;
void printAllPathsUtil(int, int, bool[], int[], int&);

public:
Graph(int V); // Constructor
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);
}
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);
}
// A recursive function to print all paths from 'u' to 'd'.
// visited[] keeps track of vertices in current path.
// path[] stores actual vertices and path_index is current
// index in path[]
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 len(string str)
{
int length = 0;
for (int i = 0; str[i] != '\0'; i++)
{
length++;

}
return length;
}
void split1 (string str, char seperator)
{
int currIndex = 0, i = 0;
int startIndex = 0, endIndex = 0;
while (i <= len(str))
{
if (str[i] == seperator || i == len(str))
{
endIndex = i;
string subStr = "";
subStr.append(str, startIndex, endIndex - startIndex);
strings1[currIndex] = subStr;
currIndex += 1;
startIndex = endIndex + 1;
}
i++;
}
}
void split2 (string str, char seperator)
{
int currIndex = 0, i = 0;
int startIndex = 0, endIndex = 0;
while (i <= len(str))
{
if (str[i] == seperator || i == len(str))
{
endIndex = i;
string subStr = "";
subStr.append(str, startIndex, endIndex - startIndex);
strings2[currIndex] = subStr;
currIndex += 1;
startIndex = endIndex + 1;
}
i++;
}
}

// Driver program
int main()
{

cout<<"Enter the two URls: \n";

string str1;
string str2 ;
cin>>str1;
cin>>str2;
char seperator = '/'; // space
split1(str1, seperator);
split2(str2, seperator);
cout <<" The split string is: ";

string C[5];int j=1;


for(int i=0;i<2;i++)
C[i]=strings1[i];
for(int i=2;i<5;i++)
{
C[i]=strings2[j];
j++;
}
for (int i = 0; i < max; i++)
{
cout << "\n i : " << i << " " << C[i];

}
cout<<"\n";
cout<<" 0 \n";
cout<<" / \\ \n";
cout<<" 1 2 \n";
cout<<" \\ \n";
cout<<" 3 \n";
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 0);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 1);
g.addEdge(2, 3);
g.addEdge(3, 2);
int s,d;
cout<<"enter source & destination no:";
cin>>s>>d;

cout << "Following are all different paths from " << s << " to " << d << endl;
g.printAllPaths(s, d);

return 0;
}

You might also like