Skip to content

Commit

Permalink
Create 寻找DAG图的根
Browse files Browse the repository at this point in the history
  • Loading branch information
zsjwll committed Apr 15, 2020
0 parents commit ec68bdb
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions 寻找DAG图的根
@@ -0,0 +1,92 @@
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
typedef struct ArcNode
{
int adjvex;
ArcNode *next;
}ArcNode;
typedef struct
{
int data;
ArcNode *first;
int sign;
}VNode;
typedef struct
{
int vexnum, arcnum;
VNode vex[100];
}ALGraph;
void creat(ALGraph &G, int tail, int head)
{
ArcNode *p;
p = new ArcNode;
p->next = G.vex[tail].first;
p->adjvex = head;;
G.vex[tail].first = p;
}
void CreatDN(ALGraph &G, int vexnum, int arcnum, int a[][100])
{
int i,j;
G.arcnum = arcnum; G.vexnum = vexnum;
for (i = 0; i < vexnum; i++)
{
G.vex[i].data = i;
G.vex[i].sign = 0;
G.vex[i].first = NULL;
}
for (i = 0; i < vexnum; i++)
for(j=0;j<vexnum;j++)
if (a[i][j] == 1)
creat(G, i, j);
}
void Vexbian(ALGraph &G, int v)
{
int w;
ArcNode *p;
G.vex[v].sign = 1;
for (p = G.vex[v].first; p; p = p->next)
{
w = p->adjvex;
Vexbian(G, w);
}
}
int Gbian(ALGraph &G)
{
int i,j,result,t;
for (i = 0; i < G.vexnum; i++)
{
result = 1;
Vexbian(G, i);
for (j = 0; j < G.vexnum; j++)
{
if(i!=j)
if (G.vex[j].sign == 0)
result = 0;
}
if (result == 1)return i;
else
{
for (t = 0; t< G.vexnum; t++)
G.vex[t].sign = 0;
}
}
}
int main()

{
int vexnum, arcnum,i,j,a[100][100],result;
ALGraph G;
char c;
scanf("please input the DAG_Graph Vertex number:%d\n", &vexnum);
scanf("please input the DAG_Graph arc number:%d\n", &arcnum);
scanf("please input the DAG_Graph adjacency matrix:\n");
for (i = 0; i < vexnum; i++)
for (j = 0; j < vexnum; j++)
scanf("%d", &a[i][j]);
CreatDN(G, vexnum, arcnum, a);
result = Gbian(G);
printf("Found a root vertex:%d", result);
return 0;

}

0 comments on commit ec68bdb

Please sign in to comment.