?? 9-6.c
字號:
#include "stdio.h"
#include <stdlib.h>
#define MaxVertexNum 100
typedef enum{FALSE,TRUE}Boolean;/*FALSE為0,TRUE為1*/
Boolean visited[MaxVertexNum];
int dfn[MaxVertexNum],low[MaxVertexNum],num;
typedef int VertexType;
typedef struct node{/*邊表結點*/
int adjvex; /*鄰接點域*/
struct node *next; /*鏈域*/
/*若要表示邊上的權,則應增加一個數據域*/
}EdgeNode;
typedef struct vnode{ /*頂點表結點*/
VertexType vertex; /*頂點域*/
EdgeNode *firstedge;/*邊表頭指針*/
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum];/*AdjList是鄰接表類型*/
typedef struct ALGraph{
AdjList adjlist;/*鄰接表*/
int n,e; /*圖中當前頂點數和邊數 */
}Graphic; /*對于簡單的應用,無須定義此類型,可直接使用AdjList類型。*/
Graphic * graph;
void CreateGraphic(Graphic *G)
{/*建立無向圖的鄰接表表示*/
int i,j,k;
EdgeNode *s;
scanf("%d%d",&G->n,&G->e); /*讀人頂點數和邊數*/
for(i=0;i<G->n;i++){/*建立頂點表*/
G->adjlist[i].vertex=getchar(); /*讀入頂點信息*/
G->adjlist[i].firstedge=NULL;/*邊表置為空表*/
}
for(k=0;k<G->e;k++){/*建立邊表*/
scanf("%d%d",&i,&j);/*讀入邊(vi,vj)的頂點對序號*/
s=(EdgeNode *)malloc(sizeof(EdgeNode)); /*生成邊表結點*/
s->adjvex=j; /*鄰接點序號為j*/
s->next=G->adjlist[i].firstedge;
G->adjlist[i].firstedge=s; /*將新結點*s插入頂點vi的邊表頭部*/
s=(EdgeNode *)malloc(sizeof(EdgeNode));
s->adjvex=i; /*鄰接點序號為i*/
s->next=G->adjlist[j].firstedge;
G->adjlist[j].firstedge=s; /*將新結點*s插入頂點vj的邊表頭部*/
}
}
void DFS(Graphic *G,int i)
{ /*以vi為出發點對鄰接表表示的圖G進行深度優先搜索*/
EdgeNode *p;
printf("visit vertex:%c",G->adjlist[i].vertex);/*訪問頂點vi*/
visited[i]=TRUE; /*標記vi已訪問*/
p=G->adjlist[i].firstedge; /*取vi邊表的頭指針*/
while(p)
{/*依次搜索vi的鄰接點vj,這里j=p->adjvex*/
if (!visited[p->adjvex])/*若vi尚未被訪問*/
DFS(G,p->adjvex);/*則以Vj為出發點向縱深搜索*/
p=p->next; //找vi的下一鄰接點
}
}
void dfnlow(int u,int v)
{/*采用dfs遍歷,計算dfn和low compute ,u表示起始結點,v 是u的父親結點*/
VertexNode ptr;
int w;
dfn[u] = low[u] = num++;
for (ptr =graph->adjlist[u]; ptr; ptr= ptr->next )
{
w = ptr->vertex;
if (dfn [w] < 0) {
dfnlow (w,u);
low[u] = _min(low[u], low[w]);
}
else if ( w != v)
low [u] = _min( low[u], def[w]);
}
}
void init (void)
{
int i;
for ( i = 0; i <MaxVertexNum; i++ ){
visited [i] = FALSE;
dfn[i] = low[i] = -1;
}
num = 0;
}
void bicon( int u, int v)
{/*計算dfn和low,以及輸出G的重連通分量*/
node_pointer ptr;
int w,x,y;
dfn[u] = low[u] = num++;
for (ptr =graph->adjlist[u]; ptr; ptr= ptr->nex){
w = ptr->vertex;
if(v!= w && dfn[w] < dfn[u])
add (&top, u, w);
if ( dfn [ w ] < 0) {
bicon(w,u);
low[u] = _min(low[u],low[w]);
if (low[w] >= dfn[u]{
printf ("New biconnected component:");
do { /* delete edge from stack */
delete (&top, &x, &y);
printf (" <&d,&d>",x,y);
} while ( !((x == u) && ( y == w)));
printf ("\n");
}
}
else if (w != v) low[u] = _min(low[u], dfn[w]);
}
}
void main(void)
{
CreateGraphic(graph);
init();
dfnlow(1,4);
bicon(1,4)
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -