IRP
IRP
traverse, find if there is a simple path (without any cycle) starting from the given source and ending at
any other vertex.
#include <stdio.h>
#include <stdlib.h>
int adj[10][10],v;
struct node
{
int ind;
struct node *next;
struct node *prev;
};
void work()
{
int i,pre_i;
pre_i=pre;
for(i=0;i<v;++i)
{
if(adj[pre_i][i]>-1000)
{
if(c[i])
{
c[i]=0;
pre=i;
p->next=vertex[i];
p->next->prev=p;
p=p->next;
dist=dist+adj[pre_i][i];
work();
}
}
}
if(dist>=k)
{
flag=1;
}
if(p!=head)
{
dist = dist-adj[p->ind][p->prev->ind];
c[p->ind]=1;
p=p->prev;
}
}
int main()
{
int i,j,l,e,a,b,d,dist,x=4;
for(j=0;j<10;++j)
{
vertex[j] = (struct node *)malloc(sizeof(struct node));
vertex[j]->ind=j;
}
flag=0;
// v=atoi(argv[1]);
scanf("%d",&v);
// e=atoi(argv[2]);
scanf("%d",&e);
//k=atoi(argv[3]);
scanf("%d",&k);
for(j=0;j<v;++j)
{
for(l=0;l<v;++l)
{
adj[j][l]=-100000;
}
c[j]=1;
}
for(j=0;j<e;++j)
{
// a=atoi(argv[x]);
scanf("%d",&a);
// b=atoi(argv[x+1]);
scanf("%d",&b);
//d=atoi(argv[x+2]);
scanf("%d",&d);
x++;
adj[a][b]=d;
adj[b][a]=d;
}
dist=0;
head=vertex[0];
p=vertex[0];
pre=0;
c[0]=0;
work();
printf("%d\n",flag);
}
Find shortest paths from source to all vertices in the given graph using Dijkstra's algorithm.
#include <limits.h>
#include <stdio.h>
#define V 5
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printSolution(int dist[])
{
printf("Vertex \t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
int main()
{
int graph[V][V],p;
for(int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
scanf("%d",&graph[i][j]);
}
}
scanf("%d",&p);
/* = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };*/
dijkstra(graph, p);
return 0;
}
#include <iostream>
using namespace std;
class Graph {
private:
int** adjMatrix;
int numVertices;
public:
// Initialize the matrix to zero
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new int*[numVertices];
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new int[numVertices];
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = 0;
}
}
// Add edges
void addEdge(int i, int j, int k)
{
adjMatrix[i][j] = k;
}
~Graph() {
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};
int main() {
int v , e , a , b , c ;
cin >> v ;
Graph g(v);
cin >> e ;
string x ;
cin >> x ;
for(int i = 0 ; i<e ; i++)
{
cin >> a >> b >> c ;
if(x=="yes")
{
g.addEdge(a-1,b-1,c) ;
}
else
{
g.addEdge(a-1,b-1,c) ;
g.addEdge(b-1,a-1,c) ;
}
}
g.toString();
}
An edge in an undirected connected graph is a bridge if and only if removing it disconnects the graph
, removing it increases number of connected components. Write a program to find the bridges in a
graph.
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
struct node
{
int node;
struct node *link;
};
while(temp!=NULL)
{
i=temp->node;
int v = i;
if (!visited[v])
{
p[v] = u;
bridgeUtil(v,visited,d,l,p);
if(l[u]<l[v])
{
l[u]=l[u];
}
else
{
l[u]=l[v];
}
if (l[v]>d[u])
{
printf("%d %d\n",u,v);
c=c+1;
}
}
else if(v!=p[u])
{
if(l[u]<l[v])
{
l[u]=l[u];
}
else
{
l[u]=l[v];}
}
temp=temp->link;
}
//printf("%d\n",c);
return c;
}
void bridge(int n)
{
int visited[n];
int d[n];
int l[n];
int p[n];
int i,c;
for(i=0;i<n;i++)
{
p[i] = 0;
visited[i] = 0;
}
for(i=0;i<n;i++)
{
if(visited[i]==0)
{
c=bridgeUtil(i, visited,d,l,p);
}
}
if(c==0){printf("0\n");}
}
for(j=0;j<n;j++)
{
if(a[i][j] != 0)
{
if(list[i]==NULL)
{
list[i] = (struct node *) malloc(sizeof(struct node));
temp = list[i];
temp->node = j;
temp->link = NULL;
}
else
{
temp = list[i];
while(temp->link != NULL)
temp = temp->link;
return list;
}
for(i=0;i<n;i++)
{
temp = list[i];
if(temp !=NULL)
printf("\nNode %d is connected to the following nodes ",i);
while(temp!=NULL)
{
printf("%d,",temp->node);
temp = temp->link;
}
}
}
Surya completed his B.Tech and got placed in a software product company in Chennai. As, he has to
relocate from Coimbatore to Chennai, he decided to buy a house. Yes, the price of houses has
decreased after EPS became CM of Tamilnadu. Surya decided to buy a house in a locality that has
burger points at each corner. As the streets are only one way there is a heavy traffic jam and it takes
time for Surya to reach a burger point.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
scanf("%d", &q);
ans = 0;
for(int i = 0; i < q; ++i) {
scanf("%d%d", &x, &y);
for(int i = 0; i <= n; ++i) { par[i] = i; sz[i] = 1; }
if(join(x, y) && cst[x][y] + kruskal() == mstc) ++ans;
}
x = gcd(q, ans);
printf("%d/%d\n", ans/x, q/x);
return 0;
}