-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ec68bdb
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |