0% found this document useful (0 votes)
79 views15 pages

IRP

The document describes an algorithm to find bridges in an undirected connected graph. It represents the graph as an adjacency list, uses depth-first search (DFS) to find articulation points and bridges. It maintains discovery and lowpoint times for each vertex during DFS. An edge between vertices u and v is a bridge if the lowest ancestor of v in the DFS tree is u.

Uploaded by

Vamshi Yadav
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)
79 views15 pages

IRP

The document describes an algorithm to find bridges in an undirected connected graph. It represents the graph as an adjacency list, uses depth-first search (DFS) to find articulation points and bridges. It maintains discovery and lowpoint times for each vertex during DFS. An edge between vertices u and v is a bridge if the lowest ancestor of v in the DFS tree is u.

Uploaded by

Vamshi Yadav
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/ 15

Given a graph, a source vertex in the graph, and a number k which is the maximum distance you can

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;
};

struct node *head,*p,*vertex[10],*t;


int c[10],dist,pre,flag,k;

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;
}

Write a program to represent the given graph in the form of a matrix.

#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;
}

// Print the martix


void toString() {
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++)
cout << adjMatrix[i][j] << " ";
cout << "\n";
}
}

~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;
};

struct node **list;


struct node ** Adjancencylist(int **,int );
void displaylist(struct node **,int );
int bridgeUtil(int,int*,int*,int*,int*);
void bridge(int);
int main()
{
int **a,n,i,j;
list=NULL;
scanf("%d",&n);
a = (int **)malloc(n*sizeof(int *));
for(i=0;i<n;i++)
{
a[i] = (int *)malloc(n*sizeof(int));
}
for(i = 0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
list=Adjancencylist(a,n);
displaylist(list,n);
printf("\nBridges in graph\n");
bridge(n);
return 0;
}
int bridgeUtil(int u, int visited[], int d[],int l[], int p[])
{
struct node *temp;
int i;
static int t= 0,c=0;
visited[u] = 1;
d[u]=l[u] =++t;
temp=list[u];

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");}
}

struct node ** Adjancencylist(int **a,int n)


{
int i,j;
struct node **list;
struct node *temp;
list=(struct node **)malloc(n * sizeof(struct node *));
for(i=0;i<n;i++)
{
list[i]=NULL;

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;

temp->link = (struct node *) malloc(sizeof(struct node));


temp = temp->link;
temp->node = j;
temp->link = NULL;
}
}
}
}

return list;
}

void displaylist(struct node **list,int n)


{
int i;
struct node * temp;

printf("Adjacency List Representation:");

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>

#define MAXN 1001


#define MAXM 1002002

typedef struct { int u, v, w; } E;


int cmp(const void *a, const void *b) { return ((E *)a)->w - ((E *)b)->w; }
int cmpc(const void *a, const void *b) { return *(char *)a - *(char *)b; }

char mst[MAXN][MAXN], cst[MAXN][MAXN], mcst[MAXN];


int t, n, m, q, ans, x, y,
par[MAXN], sz[MAXN];
E e[MAXM];

int gcd(int a, int b) {


int tmp;
while(b) {
tmp = a%b;
a = b;
b = tmp;
}
return a;
}
int find(int p) { while(p != par[p]) p = par[p]; return p; }
int join(int p, int q) {
int rp = find(p), rq = find(q);
if(rp == rq) return 0;
if(sz[rp] < sz[rq]) {
par[rp] = rq;
sz[rq] += sz[rp];
} else {
par[rq] = rp;
sz[rp] += sz[rq];
}
return 1;
}
int kruskal() {
qsort(e, m, sizeof(E), cmp);
int mstc = 0;
for(int i = 0; i < m; ++i) {
if(join(e[i].u, e[i].v)) {
mstc += e[i].w;
}
}
return mstc;
}
int main() {

scanf("%d%d", &n, &m);


for(int i = 0; i < m; ++i) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
cst[e[i].u][e[i].v] = cst[e[i].v][e[i].u] = e[i].w;
}

for(int i = 0; i <= n; ++i) { par[i] = i; sz[i] = 1; }


int mstc = kruskal();
// printf("%d\n", mstc);

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;
}

You might also like