无向图算法——AB两点是否连通
(2008-08-04 16:42:13)
标签:
爱在中国行算法it |
分类: 编程珠玑 |
本文主要讨论无向图AB两点是否连通的算法。
分别有邻接表和邻接矩阵存储两个版本。
主要函数列表如下:
int IsConnected(AGraph *G,int A,int B)
int IsConnected(MGraph M,int A,int B)
//If this code works ,it is written by Dannyboy
//compiler:Microsoft VC++
#include <stdio.h>
#define N 7
#define STACKLEN 100
int A[7][7]={
{0,1,0,0,0,0,0},//0
{1,0,1,1,0,0,0},//1
{0,1,0,0,0,0,0},//2
{0,1,0,0,1,1,0},//3
{0,0,0,1,0,1,1},//4
{0,0,0,1,1,0,0},//5
{0,0,0,0,1,0,0} //6
};
typedef struct ArcCell
{
}ArcCell;
typedef struct
{
}MGraph;
typedef struct ArcNode
{
}ArcNode;
typedef struct VNode
{
int data;
ArcNode *firstarc;
}VNode,adjList[N];
typedef struct
{
}AGraph;
void CreateMGraph(MGraph& M)
{
}
void CreateAdjFromMat(AGraph *G,MGraph A)
{
}
int IsConnected(AGraph *G,int A,int B)
{
typedef struct node
{
}node;
int i,j;
int connected[N];int visited[N];
for(i=0;i<N;i++)
{connected[i]=0;visited[i]=0;}
int Stack[STACKLEN];
int top=-1;
ArcNode *p;
p=G->vertices[A].firstarc;
while(p!=NULL)
{
}
while(top>=0)
{
int out=Stack[top];
top--;
visited[out]=1;
ArcNode *p;
p=G->vertices[out].firstarc;
while(p!=NULL)
{