Skip to content

Commit

Permalink
Added lab-11
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatkhanna1999 committed Jan 7, 2019
1 parent c3d1ec5 commit 3a13917
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 0 deletions.
88 changes: 88 additions & 0 deletions Lab-11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Lab-10

## Question-01
Given a directed graph with n nodes numbered 0 to n-1, and m edges, print the vertices visited while performing a BFS.

Input Format

First line contains two space separated integers, n and m.

The m consecutive lines contains two space separated integers u and v, denoting an edge between vertex u and vertex v.

The last line contains a single integer s denoting the source vertex.

Constraints

No Constraints

Output Format

Print space separated integers denoting the vertices visited while performing a BFS.

Sample Input 0

4 6
0 1
0 2
1 2
2 0
2 3
3 3
2
Sample Output 0

2 0 3 1
Explanation 0

image

Step 1: Starting from 2, you'll reach 0 and 3

Step 2: From 0 you'll reach 1 and 2

Step 3: From 3 you'll reach 3 again because of the self loop

Step 4: From 1, you'll 2

So the order of vertices visited while performing a BFS is 2 0 3 1

## Question-02
Given a directed graph with n nodes numbered 0 to n-1, and m weighted edges, print the vertices visited while performing a DFS, along with the wieght of the edge traversed.

Note: For the source vertex, print -1 as the weight of the edge traversed.

Input Format

First line contains two space separated integers, n and m.

The m consecutive lines contains three space separated integers u, v and w, denoting an edge between vertex u and vertex v with weight w.

The last line contains a single integer s denoting the source vertex.

Constraints

No Constraints

Output Format

Print two space separated integers in each line denoting the vertices visited while performing a DFS and the wieght of the edge traversed respectively.

Sample Input 0

4 6
0 1 1
0 2 2
1 2 3
2 0 4
2 3 5
3 3 6
2
Sample Output 0

2 -1
0 4
1 1
3 5
Explanation 0

image
74 changes: 74 additions & 0 deletions Lab-11/lab-11q1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

//Handle:cyber_rajat

ll Edge[100][100]={0};
bool visited[100];

void BreadthFirstSearch(ll start,ll n)
{
queue<ll> Nodes;
Nodes.push(start);
visited[start]=true;

while(Nodes.size()!=0)
{
ll vertix=Nodes.front();
cout<<vertix<<" ";
Nodes.pop();

for(ll i=0;i<n;i++)
{
if(Edge[vertix][i]==1)
{
if(!visited[i])
{
Nodes.push(i);
visited[i]=true;
}
}
}
}

return ;
}

int main()
{
ll n,m;
cin>>n>>m;
for(ll i=0;i<m;i++)
{
ll u,v;
cin>>u>>v;
Edge[u][v]=1;
}
ll start;
cin>>start;
for(ll i=0;i<n+5;i++)
visited[i]=false;
BreadthFirstSearch(start,n);
for(ll i=0;i<n;i++)
{
if(!visited[i])
BreadthFirstSearch(i,n);
}
}
69 changes: 69 additions & 0 deletions Lab-11/lab-11q2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

//Handle:cyber_rajat
bool visited[100];
ll x;
vector<pair<ll,ll> > arr[100];

void DepthFirstSearch(ll data,ll start,ll n)
{
cout<<start<<" "<<data<<endl;;
visited[start]=true;

for(ll i=0;i<arr[start].size();i++)
{
if(!visited[arr[start][i].first])
{
//if(start==x)
// cout<<"-1"<<endl;
//else
// cout<<arr[prev][start].second<<endl;
//visited[arr[start][i].first]=true;
DepthFirstSearch(arr[start][i].second,arr[start][i].first,n);
}
}
return ;
}

int main()
{
ll n,m;
cin>>n>>m;
for(ll i=0;i<n+5;i++){
visited[i]=(false);
}
for(ll i=0;i<m;i++)
{
ll u,v,w;
cin>>u>>v>>w;
arr[u].pb(mp(v,w));
}
ll start;
cin>>start;
ll x=-1;
DepthFirstSearch(x,start,n);
for(ll i=0;i<n;i++)
{
if(!visited[i])
DepthFirstSearch(x,i,n);
}

}

0 comments on commit 3a13917

Please sign in to comment.