c邻接矩阵存储
⑴ 数据结构C语言版 图的遍历 DFS和BFS算法,用邻接矩阵储存 急阿在线等 求大神指点
#include <iostream>
#include<string.h>
#include<stack>
#include<queue>
const int Max=100;
const int VISITED=101010;
const int UNVISITED=111111;
const int AFFINITY=101010;
const int INFINITY=111111;
using namespace std;
class Edge
{
public:
int start;
int end;
int weight;
Edge(int st=0,int en=0,int w=0):start(st),end(en),weight(w){}
bool operator>(Edge oneEdge){return weight>oneEdge.weight?true:false;}
bool operator<(Edge oneEdge){return weight<oneEdge.weight?true:false;}
bool operator!=(Edge oneEdge)
{
if(weight!=oneEdge.weight||start!=oneEdge.start||end!=oneEdge.end)
return true;
return false;
}
};
class AdjGraf
{
private:
int verticesNum;
int edgeNum;
int **matrix;
int *Mark;
public:
AdjGraf(int vert)
{
int i=0,j=0;
verticesNum=vert;
matrix=(int**)new int*[vert];
for(i=0;i<vert;i++)
matrix[i]=new int[vert];
Mark=new int[vert];
for(i=0;i<vert;i++)
for(j=0;j<vert;j++)
{
matrix[i][j]=0;
}
for( int m=0;m<verticesNum;m++)
Mark[m]=UNVISITED;
}
~AdjGraf();
//返回与顶点oneVertex相关联的第一条边
Edge FirstEdge(int oneVertex);
//返回与边PreEdge有相同关联顶点oneVertex的下一条边
Edge NextEdge( Edge preEdge);
//添加一条边
void setEdge(int fromVertex,int toVertex,int weight);
//删一条边
void delEdge(int fromVertex,int toVertex);
//如果oneEdge是边则返回TRUE,否则返回FALSE
bool IsEdge( Edge oneEdge)
{
if(oneEdge.start>=0&&oneEdge.start<verticesNum&&
oneEdge.end>=0&&oneEdge.end<verticesNum)
return true;
else return false;
}
//返回边oneEdge的始点
int FromVertex(Edge oneEdge){return oneEdge.start;}
//返回边oneEdge的终点
int ToVertex(Edge oneEdge){return oneEdge.end;}
//返回边oneEdge的权
int Weight(Edge oneEdge){return oneEdge.weight;}
void visit(int i){cout<<i+1<<" ";}
void BFS(int i=1);
void DFS(int i);
void DFSTraverse(int v);
void DFSNoReverse(int f=1);
Edge UNVISITEDEdge(int f);
};
AdjGraf::~AdjGraf()
{
for(int i=0;i<verticesNum;i++)
delete[]matrix[i];
delete[]matrix;
}
Edge AdjGraf::FirstEdge(int oneVertex)
{ int i;
Edge tempEdge;
tempEdge.start=oneVertex;
for( i=0;i<verticesNum;i++)
if(matrix[oneVertex][i]!=0)
break;
tempEdge.end=i;
tempEdge.weight=matrix[oneVertex][i];
return tempEdge;
}
Edge AdjGraf ::NextEdge( Edge preEdge)
{
Edge tempEdge;
tempEdge.start=preEdge.start;
int i=0;
for(i=preEdge.end+1;i<verticesNum;i++)
if(matrix[preEdge.start][i]!=0)
break;
tempEdge.end=i;
tempEdge.weight=matrix[preEdge.start][i];
return tempEdge;
}
void AdjGraf::setEdge(int fromVertex,int toVertex,int weight)
{
if(matrix[fromVertex-1][toVertex-1]==0)
edgeNum++;
matrix[fromVertex-1][toVertex-1]=weight;
}
void AdjGraf::delEdge(int fromVertex,int toVertex)
{
if(matrix[fromVertex][toVertex]==0)
edgeNum--;
matrix[fromVertex][toVertex]=0;
}
/*************递归实现深度优先****************/
void AdjGraf::DFS(int i)
{
visit(i);
Mark[i]=VISITED;
for(Edge e=FirstEdge(i);IsEdge(e);e=NextEdge(e))
if(Mark[ToVertex(e)] == UNVISITED)
DFS(ToVertex(e));
}
void AdjGraf::DFSTraverse(int v)
{
v--;
int i;
for(i=0;i<verticesNum;i++)
Mark[i]=UNVISITED;
for(i=v;i<v+verticesNum;i++)
if (Mark[i]== UNVISITED)
DFS(i);
}
Edge AdjGraf::UNVISITEDEdge(int f)
{ int i;
for( Edge e=FirstEdge(f);IsEdge(e);e=NextEdge(e))
if(Mark[e.end]==UNVISITED)
return e;
return Edge(verticesNum,verticesNum,0) ;
}
/*************非递归实现深度优先**************/
void AdjGraf::DFSNoReverse(int f)
{
f--;
int i,counter=0,j,flag;
stack<int>Temp;
for(i=0;i<verticesNum;i++)
Mark[i]=UNVISITED;
flag=f;
while(counter<12)
{
while(flag!=verticesNum&&IsEdge(UNVISITEDEdge(flag))||!Temp.empty())
{
// Edge tempEdge=UNVISITEDEdge(j);
while(flag!=verticesNum&&Mark[flag]==UNVISITED)
{
visit(flag);
Mark[flag]=VISITED;
Temp.push(flag);
flag=UNVISITEDEdge(flag).end;
}
if(!Temp.empty())
{
flag=UNVISITEDEdge(Temp.top()).end;
Temp.pop();
}
}
if(Mark[counter]==UNVISITED) flag=counter;
else counter++;
}
}
/*************非递归实现广度优先**************/
void AdjGraf::BFS(int v)
{
int i;
v--;
for( i=0;i<verticesNum;i++)
Mark[i]=UNVISITED;
queue<int>tempqueue;
i=0;
/*********v先从指定位置开始,然后从v=0,1,2......
依次检查是否有孤立结点*****************/
while(i<verticesNum)
{
tempqueue.push(v);
while(!tempqueue.empty())
{
v=tempqueue.front();
tempqueue.pop();
if(Mark[v]==UNVISITED)
{
visit(v);
Mark[v]=VISITED;
for(Edge e=FirstEdge(v);IsEdge(e);e=NextEdge(e))
{
v=ToVertex(e);
tempqueue.push(v);
}
}
}
/***********防止出现孤立点****************/
if(Mark[i]==VISITED) i++;
else v=i;
}
}
int main()
{
AdjGraf Graph(12);
Graph.setEdge(1,2,1);
Graph.setEdge(2,1,1);
Graph.setEdge(1,3,5);
Graph.setEdge(3,1,5);/** V1 V12 V11 */
Graph.setEdge(2,4,3);/** / \ / \ */
Graph.setEdge(4,2,3);/** v2 v3 V10 V9 */
Graph.setEdge(2,5,7);/** / \ / \ */
Graph.setEdge(5,2,7);/** v4 v5 v6-v7 */
Graph.setEdge(4,8,4);/** \ / */
Graph.setEdge(8,4,4);/** v8 */
Graph.setEdge(5,8,3);
Graph.setEdge(8,5,3);
Graph.setEdge(3,6,2);
Graph.setEdge(6,3,2);
Graph.setEdge(3,7,1);
Graph.setEdge(7,3,1);
Graph.setEdge(6,7,6);
Graph.setEdge(7,6,6);
Graph.setEdge(12,9,6);
Graph.setEdge(9,12,6);
Graph.setEdge(12,10,6);
Graph.setEdge(10,12,6);
Graph.setEdge(11,11,6);
cout<<"DFSTraverse:"<<endl;
Graph.DFSTraverse(3);
cout<<endl;
cout<<"DFSNoReverse:"<<endl;
Graph.DFSNoReverse(3);
cout<<endl;
cout<<"BFS:"<<endl;
Graph.BFS(3);
cout<<endl;
return 0;
}
以上代码运行环境codeblocks 程序采用DFS递归算法 DFS非递归算法 BFS非递归算法
望采纳~
⑵ C语言数据结构算法,连通图的深度优先搜索,存储结构是邻接矩阵,空怎么填啊
voiddfs(inta[][],intv,intn)
{
access(v);
visited[v]=1;
w=0;
while(w<=n&&a[v][w]==0)w++;
while(w<=n)
{
if(visited[w]==0)dfs(a,w,n);
w++;
while((w<=n)&&a[v][w]==0)w++;
}
}
第一空:visited[w]==0
第二空:dfs(a,w,n)
⑶ C语言数据结构实现1.有向图的图的邻接矩阵存储、深度和广度优先搜索遍历、拓扑排序求解。
#include"iostream.h"
const int n=8;
const int e=15;
typedef int elemtype ;
bool visited[n+1];
class link
{
public:
elemtype data;
link *next;
};
class graph
{
public:
link a[n+1];
void creatlink()
{
int i,j,k;
link *s;
for(i=1;i<=n;i++)
{
a[i].data=i;
a[i].next=NULL;
}
for(k=1;k<=e;k++)
{
cout<<"请输入一条边";
cin>>i>>j;
cout<<endl;
s=new link;
s->data=j;
s->next=a[i].next;
a[j].next=s;
}
}
void dfs1(int i)
{
link *p;
cout<<a[i].data<<" ";
visited[i]=true;
p=a[i].next;
while(p!=NULL)
{
if(!visited[p->data])
dfs1(p->data);
p=p->next;
}
}
void bfs1(int i)
{
int q[n+1];
int f,r;
link *p;
f=r=0;
cout<<a[i].data<<" ";
visited[i]=true;
r++;q[r]=i;
while(f<r)
{
f++;i=q[r];
p=a[i].next;
while(p!=NULL)
{
if(!visited[p->data])
{
cout<<a[p->data].data<<" ";
visited[p->data]=true;
r++;q[r]=p->data;
}
p=p->next;
}
}
}
};
void main()
{
link *p;int yn=1;
graph g;
g.creatlink();
while(yn){
for(int i=1;i<=n;i++)
{
p=g.a[i].next;
cout<<g.a[i].data<<"->";
while(p->next!=NULL)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<p->data<<endl;
}
for(i=1;i<=n;i++)
visited[i]=false;
cout<<"输入深度优先搜索开始访问的顶点";
cin>>i;
cout<<endl;
cout<<"从"<<i<<"出发的深度优先搜索遍历序列为"<<endl;
g.dfs1(i);
cout<<endl;
for(i=1;i<=n;i++)
visited[i]=false;
cout<<"请输入广度优先搜索开始访问的顶点";
cin>>i;
cout<<endl;
cout<<"从"<<i<<"出发的广度优先搜索遍历序列为"<<endl;
g.bfs1(i);
cout<<endl;
cout<<"继续遍历吗(1/0)?";
cin>>yn;
}
}
/*图为:
1 2 3
5 4
6 7 8*/
⑷ 用邻接矩阵储存图,所占用的储存空间大小
图的邻接矩阵存储所占用空间大小只与顶点个数有关,更准确地说,设顶点n个,则与n^2成正比(n的平方)
⑸ 存储图时,哪些情况用邻接矩阵方便
如果边比较少的情况下,用邻接矩阵节省存储空间。边比较多就可以选择邻接矩阵
⑹ c++利用邻接矩阵存储方法实现图的存储与输出。
复制粘贴即可
#include <iostream>
using namespace std;
//*****stack.h
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
template<class QElemType>
class stack
{
public:
void InitStack();
void DestroyStack();
void ClearStack();
Status StackEmpty();
Status StackLength();
void GetTop(QElemType & e);
void Push(QElemType e);
void Pop(QElemType & e);
private:
struct SqStack{
QElemType *base;
QElemType *top;
int stacksize;
}S;
};
//******stack.cpp------
template<class QElemType>
void stack<QElemType>::InitStack()
{
S.base = (QElemType *)malloc(STACK_INIT_SIZE * sizeof(QElemType));
if(!S.base) exit(0);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}
template <class QElemType>
void stack<QElemType>::DestroyStack()
{
free(S.base);
}
template <class QElemType>
void stack<QElemType>::ClearStack()
{
S.top = S.base;
}
template <class QElemType>
Status stack<QElemType>::StackEmpty()
{
if(S.top == S.base) return 1;
else return 0;
}
template <class QElemType>
Status stack<QElemType>::StackLength()
{
return (S.top - S.base);
}
template <class QElemType>
void stack<QElemType>::GetTop(QElemType & e)
{
if(S.top != S.base)
e = *(S.top - 1);
else cout << "ERROR" << endl;
}
template <class QElemType>
void stack<QElemType>::Push(QElemType e)
{
if(S.top - S.base >= S.stacksize)
{
S.base = (QElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT) * sizeof(QElemType));
if(!S.base) exit(0);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
}
template <class QElemType>
void stack<QElemType>::Pop(QElemType & e)
{
if(S.top == S.base) cout << "ERROR" << endl;
else
e = * --S.top;
}
//**********stack.cpp End
template <class TElemType>
class Graph
{
public:
void CreateUDN();
void DestroyUDN();
void CreateAlgraph();
void DestroyAlgraph();
void DFS(int v,bool *visited);
void DFSTraverse();
void Minispantree_prim(); //PRIM算法求最小生成树
void Shortestpath_DIJ(TElemType data1,TElemType data2); //对环不适用,如从V1到V1就不适用
void Shortestpath_FLOYD(TElemType data1,TElemType data2);
private:
template <class TElemType>
struct Mgraph{
int vexnum;
int arcnum;
TElemType *vertex;
int **AdjMatrix;
};
Mgraph<TElemType> gph; //邻接矩阵存储
struct Arcnode
{
int adjvex;
Arcnode *nextarc;
float weight;
};
template <class TElemType>
struct Vexnode
{
TElemType data;
Arcnode *firarc;
};
struct ALgraph
{
int vexnum;
int arcnum;
bool kind;
Vexnode<TElemType> *vex;
};
ALgraph algraph; //邻接表存储
};
//*********Graph.cpp
template <class TElemType>
void Graph<TElemType>::CreateUDN()
{
cout << "输入无向网的顶点数和边数:" << endl;
cin >> gph.vexnum >> gph.arcnum;
gph.vertex = (TElemType *)malloc(gph.vexnum * sizeof(TElemType));
int i,j,m,n; //m,n表示顶点信息对应的序号,w是权值
int w;
TElemType v1,v2;
cout << "输入顶点信息:" << endl;
for(i = 0;i < gph.vexnum;i++)
cin >> gph.vertex[i];
gph.AdjMatrix = (int **)malloc(gph.vexnum * sizeof(int *));
for(i = 0;i < gph.vexnum;i++)
gph.AdjMatrix[i] = (int *)malloc(gph.vexnum * sizeof(int));
for(i = 0;i < gph.vexnum;i++)
for(j = 0;j < gph.vexnum;j++)
gph.AdjMatrix[i][j] = INT_MAX; //INT_MAX
cout << "输入一条边依附的两点及权值:" << endl;
for(int k = 0;k < gph.arcnum;k++)
{
cin >> v1 >> v2 >> w;
for(i = 0;i < gph.vexnum;i++)
{
if(v1 == gph.vertex[i]) m = i;
if(v2 == gph.vertex[i]) n = i;
}
gph.AdjMatrix[m][n] = gph.AdjMatrix[n][m] = w;
}
}
template <class TElemType>
void Graph<TElemType>::DestroyUDN()
{
free(gph.vertex);
for(int i = 0;i < gph.vexnum;i++)
free(gph.AdjMatrix[i]);
free(gph.AdjMatrix);
}
template <class TElemType>
void Graph<TElemType>::CreateAlgraph()
{
int i,j,m,n;
float w;
TElemType v1,v2;
Arcnode *p;
cout << "输入图类型(1是无向图,0是有向图):" << endl;
cin >> algraph.kind;
cout << "输入顶点数和边数:" << endl;
cin >> algraph.vexnum >> algraph.arcnum;
algraph.vex = (Vexnode<TElemType> *)malloc(algraph.vexnum * sizeof(Vexnode<TElemType>));
cout << "输入顶点信息:" << endl;
for(i = 0;i < algraph.vexnum;i++)
{
cin >> algraph.vex[i].data;
algraph.vex[i].firarc = NULL;
}
if(algraph.kind)
{
cout << "输入各边依附的两点和权值:" << endl;
for(i = 0;i < algraph.arcnum;i++)
{
cin >> v1 >> v2 >>w;
for(j = 0;j < algraph.vexnum;j++)
{
if(v1 == algraph.vex[j].data) m = j;
if(v2 == algraph.vex[j].data) n = j;
}
p = (Arcnode *)malloc(2*sizeof(Arcnode));
p[0].adjvex = n;p[0].weight = w;
p[1].adjvex = m;p[1].weight = w;
p[0].nextarc = algraph.vex[m].firarc;algraph.vex[m].firarc = p;
p[1].nextarc = algraph.vex[n].firarc;algraph.vex[n].firarc = ++p;
}
}
else
{
cout << "输入各边的弧尾与弧头结点及有向边的权值:" << endl;
for(i = 0;i < algraph.arcnum;i++)
{
cin >> v1 >> v2 >> w;
for(j = 0;j < algraph.vexnum;j++)
{
if(v1 == algraph.vex[j].data) m = j;
if(v2 == algraph.vex[j].data) n = j;
}
p = (Arcnode *)malloc(sizeof(Arcnode));
p->adjvex = n;p->weight = w;
p->nextarc = algraph.vex[m].firarc;algraph.vex[m].firarc = p;
}
}
} //构造完成
template <class TElemType>
void Graph<TElemType>::DestroyAlgraph()
{
int i;
Arcnode *p,*q;
for(i = 0;i < algraph.vexnum;i++)
{
p = algraph.vex[i].firarc;
if(p)
{
q = p->nextarc;
while(q)
{
free(p);
p = q;
q = q->nextarc;
}
free(p);
}
}
free(algraph.vex);
}
template <class TElemType>
void Graph<TElemType>::DFS(int v,bool *visited)
{
cout << algraph.vex[v].data << endl;
visited[v] = true;
Arcnode *p;
int v1;
for(p = algraph.vex[v].firarc;p;p = p->nextarc)
{
v1 = p->adjvex;
if(!visited[v1]) DFS(v1,visited);
}
}
template <class TElemType>
void Graph<TElemType>::DFSTraverse()
{
int i,v;
bool *visited = (bool *)malloc(algraph.vexnum * sizeof(bool));
for(i = 0;i < algraph.vexnum;i++)
visited[i] = false;
for(v = 0;v < algraph.vexnum;v++)
if(!visited[v]) DFS(v,visited);
free(visited);
} //EndDFSTraverse
template <class TElemType>
void Graph<TElemType>::Minispantree_prim()
{
struct closedge
{
int adjvex;
int lowcost;
};
closedge *edge = (closedge *)malloc(gph.vexnum * sizeof(closedge));
int i,j,k = 0,u;
int min;
for(i = 0;i < gph.vexnum;i++)
{
if(i != k)
{
edge[i].adjvex = 0;
edge[i].lowcost = gph.AdjMatrix[k][i];
}
}
edge[k].lowcost = 0;
cout << "最小生成树的边如下:" << endl;
for(i = 1;i < gph.vexnum;i++)
{
min = INT_MAX;
for(j = 0;j < gph.vexnum;j++)
if(edge[j].lowcost != INT_MAX &&edge[j].lowcost != 0 && edge[j].lowcost < min)
{
min = edge[j].lowcost;
k = j;
}
u = edge[k].adjvex;
edge[k].lowcost = 0;
cout << "(" << gph.vertex[u] << "," << gph.vertex[k] << ")" << " ";
for(j = 0;j < gph.vexnum;j++)
if(gph.AdjMatrix[j][k] < edge[j].lowcost)
{
edge[j].lowcost = gph.AdjMatrix[j][k];
edge[j].adjvex = k;
}
}
free(edge);
cout << endl;
}
template <class TElemType>
void Graph<TElemType>::Shortestpath_DIJ(TElemType data1,TElemType data2)
{
int i,j,v,u,k,min;
stack<int> S;
S.InitStack();
int *spath = (int *)malloc(gph.vexnum * sizeof(int));
int *pathrecord = (int *)malloc(gph.vexnum * sizeof(int));
bool *visited = (bool *)malloc(gph.vexnum * sizeof(bool));
for(i = 0;i < gph.vexnum;i++) visited[i] = false;
for(i = 0;i < gph.vexnum;i++)
{
if(data1 == gph.vertex[i]) v = i;
if(data2 == gph.vertex[i]) u = i;
}
for(i = 0;i < gph.vexnum;i++)
{
spath[i] = gph.AdjMatrix[v][i];
pathrecord[i] = v;
}
spath[v] = 0;visited[v] = true;pathrecord[v] = -1;
for(i = 1;i < gph.vexnum;i++)
{
min = INT_MAX;
for(j = 0;j < gph.vexnum;j++)
{
if(!visited[j])
{
if(spath[j] < min) {min = spath[j];k = j;}
}
}
visited[k] = true;
for(j = 0;j < gph.vexnum;j++)
if(!visited[j] && gph.AdjMatrix[k][j] < INT_MAX && spath[k]+gph.AdjMatrix[k][j] < spath[j])
{
spath[j] = spath[k]+gph.AdjMatrix[k][j];
pathrecord[j] = k;
}
}
free(visited);
cout << spath[u] << endl;
S.Push(u);
for(v = pathrecord[u];v != -1;v = pathrecord[v])
S.Push(v);
while(!S.StackEmpty())
{
S.Pop(i);
cout << gph.vertex[i] << " ";
}
cout << endl;
S.DestroyStack();
free(spath);
free(pathrecord);
}
template <class TElemType>
void Graph<TElemType>::Shortestpath_FLOYD(TElemType data1,TElemType data2)
{
int i,j,k,v,u,m;
int **D = (int **)malloc(gph.vexnum * sizeof(int *));
bool ***path = (bool ***)malloc(gph.vexnum * sizeof(bool **));
for(i = 0;i < gph.vexnum;i++)
{
D[i] = (int *)malloc(gph.vexnum * sizeof(int));
path[i] = (bool **)malloc(gph.vexnum * sizeof(bool *));
if(data1 == gph.vertex[i]) v = i;
if(data2 == gph.vertex[i]) u = i;
}
for(i = 0;i < gph.vexnum;i++)
for(j = 0;j < gph.vexnum;j++)
path[i][j] = (bool *)malloc(gph.vexnum *sizeof(bool));
for(i = 0;i < gph.vexnum;i++)
for(j = 0;j < gph.vexnum;j++)
{
D[i][j] = gph.AdjMatrix[i][j];
for(k = 0;k < gph.vexnum;k++)
path[i][j][k] = false;
if(D[i][j] < INT_MAX)
{
path[i][j][i] = true;path[i][j][j] = true;
}
}
for(k = 0;k < gph.vexnum;k++)
for(i = 0;i < gph.vexnum;i++)
for(j = 0;j < gph.vexnum;j++)
if(D[i][k] != INT_MAX && D[k][j] != INT_MAX && D[i][k]+D[k][j] < D[i][j])
{
D[i][j] = D[i][k] + D[k][j];
for(m = 0;m < gph.vexnum;m++)
path[i][j][m] = path[i][k][m] || path[k][j][m];
}
cout << "从" << gph.vertex[v] << "到" << gph.vertex[u] << "的最短路径及经过的点如下:" << endl;
cout << D[v][u] << endl;
for(i = 0;i < gph.vexnum;i++)
if(path[v][u][i] == true) cout << i << " ";
cout << endl;
for(i = 0;i < gph.vexnum;i++)
{
free(D[i]);
free(path[i]);
}
free(D);
free(path);
}
//***********end Graph
int main()
{
Graph<int> gph;
gph.CreateUDN();
gph.Minispantree_prim();
int data1,data2;
cout << "输入起点和终点:" << endl;
cin >> data1 >> data2;
gph.Shortestpath_DIJ(data1,data2);
//gph.Shortestpath_FLOYD(data1,data2);
gph.DestroyUDN();
return 0;
}
功能函数都实现了,可以自己在源程序中调用函数实现各种功能。
⑺ C语言。如果两个顶点之间有多条边,还可以用邻接矩阵存储吗
可以,如果a到b有1条边就在矩阵ab位置置1;如果a到b有2条边就在矩阵ab位置置2;如果a到b有0条边就在矩阵ab位置置0;
⑻ 数据结构 用C语言编程:求邻接矩阵存储结构的有向图G中各结点的出度
对每个结点所对应的那一列,中的所有1加起来,就是出度。(邻接矩阵中存的是0, 1)
入度的计算也是类似的。
V : 结点集合。v_i (i = 0, n-1), n = |V|.
E : 边集合。表示为n*n的邻接矩阵。
E[i, j] = { if v_i -> v_j 存在有向边,1。else 0 }
求结点v_i的出度(伪码):
for (i = 0; i < n-1; i++) {
degree_sum = 0;
for (j = 0; j < n-1; j++) {
if (E[i][j] == 1)
degree_sum++;
}
}
⑼ C语言 邻接矩阵和邻接表
/**********************邻接矩阵*****************/
#include<bits/stdc++.h>//邻接矩阵
using namespace std;
const int N=300;
int Map[N][N]={0};//邻接矩阵
int book[N]={0};//结点标记数组(1表示该点访问过了;0未访问过)
int n,m;
void dfs(int x)//深度遍历
{
for(int i=1;i<=n;i++)
{//book[i]==0:i未被访问过
// Map[x][i]==1:x到i有边连接
if(book[i]==0&&Map[x][i]==1)
{
book[i]=1;//访问标记
printf("->[%d]",i);
dfs(i);//前往下一个结点i
}
}
}
void bfs(int x)
{
int q[N]={0};
int fornt=0;
int rear=0;
q[rear++]=x;//源点入队
while(fornt<rear)
{
int k=q[fornt++];//出队
for(int i=1;i<=n;i++)
{//扩展一个点周围可以访问的点
if(book[i]==0&&Map[k][i]==1)
{
printf("->[%d]",i);
book[i]=1;//访问标记
q[rear++]=i;//入队
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);//n个结点,m条边
for(int i=0;i<m;i++)
{//构造无向图
int x,y;//输入2个结点;x->y;y->x;
scanf("%d%d",&x,&y);
Map[x][y]=1;//1代表x,y邻接
Map[y][x]=1;
}
book[1]=1;//标记结点1
printf("深度遍历路径 [%d]",1);
dfs(1);//从结点1深度遍历 (起始点可以随便选,1~n)
printf(" ");
memset(book,0,sizeof(book));//标记数组置0,用于广度遍历标记
printf("广度遍历路径 [%d]",1);
book[1]=1;//标记结点1
bfs(1);//从结点1广度遍历 (起始点可以随便选,1~n)
return 0;
}
/*5个结点,7条边
5 7
1 3
1 2
1 4
2 4
2 5
3 5
4 5
*/
/*************************邻接表*************************************/
#include<bits/stdc++.h>//万能头文件,C/C++都能用,包含了所有的头文件
using namespace std;//邻接表
const int N=300;
typedef struct st{
int v;//表头能到达的结点
struct st *next;//指向下一个结点的指针域
}*link,AK;//定义结点类型
struct sx{
AK *next;//表头指针域
}s[N];//n个结点,n个表头
int book[N]={0};//结点标记数组(1表示该点访问过了;0未访问过)
int n,m;
void create(int x,int y)
{//前插法构建邻接表
link p;
p=new AK;//开辟新结点
p->v=y;
p->next=s[x].next;
s[x].next=p;
}
void fun()
{//表头指针赋空(这一步至关重要,没有这一步无法建表)
for(int i=1;i<=n;i++)
s[i].next=NULL;
}
void dfs(int x)//深度遍历邻接表
{
link p=s[x].next;
while(p)
{
if(book[p->v]==0)
{
printf("->[%d]",p->v);
book[p->v]=1;
dfs(p->v);
}
p=p->next;
}
}
void bfs(int x)//广度遍历邻接表
{
int q[N]={0};//队列
int fornt=0;
int rear=0;
q[rear++]=x;
while(fornt<rear)
{
int k=q[fornt++];
link p=s[k].next;
while(p)
{
if(book[p->v]==0)
{
printf("->[%d]",p->v);
book[p->v]=1;
q[rear++]=p->v;
}
p=p->next;
}
}
}
void input()//打印邻接表
{//首列是表头(其后尾随的是与其邻接的结点)
//n个表头
link p;
for(int i=1;i<=n;i++)
{
p=s[i].next;
printf("[%d]",i);
while(p)
{
printf("->[%d]",p->v);
p=p->next;
}
cout<<endl;
}
}
int main()
{
fun();//调用表头置空
scanf("%d%d",&n,&m);//n个结点,m条边
for(int i=0;i<m;i++)
{//构造无向邻接表
int x,y;//输入2个结点;x->y;y->x;
scanf("%d%d",&x,&y);
create(x,y);//构建有向邻接表,只调用一个;
create(y,x);//构建无向邻接表,只调用两个;
}
book[1]=1;//标记结点1
printf("深度遍历路径 [%d]",1);
dfs(1);//从结点1深度遍历 (起始点可以随便选,1~n)
printf(" ");
memset(book,0,sizeof(book));//标记数组置0,用于广度遍历标记
printf("广度遍历路径 [%d]",1);
book[1]=1;//标记结点1
bfs(1);//从结点1广度遍历 (起始点可以随便选,1~n)
printf(" ");
printf("打印邻接表结构 ");
input();//打印邻接表
return 0;
}
/*5个结点,7条边
5 7
1 3
1 2
1 4
2 4
2 5
3 5
4 5
*/
⑽ c/c++图的邻接矩阵存储结构
关于图的深度优先遍历代码如下:
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TURE 1
#define OK 1
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20 //最大顶点个数
typedef struct ArcCell{
int adj; //顶点关系类型。用1或0表示相邻与否
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //二维数组
typedef struct {
char vexs[MAX_VERTEX_NUM]; //顶点向量
AdjMatrix arcs; //邻接矩阵
int vexnum,arcnum; //顶点数和弧数
}MGraph;
int LocateVex(MGraph G,char u)
{ //若图G中存在顶点u,则返回该顶点在图中的位置;否则返回-1
int i;
for(i=0;i<G.vexnum;i++)
if(G.vexs[i]==u)
return i;
else return -1;
}
int CreateDG(MGraph G){ //构造有向图G
int i,j,k;
char va,vb;
printf("请输入有向图G的顶点数 弧数:");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入%d个顶点的值(<5个字符):\n",G.vexnum);
for(i=0;i<G.vexnum;i++) scanf("%s",&G.vexs[i]); //构造顶点向量
for(i=0;i<G.vexnum;i++) //初始化邻接矩阵
for(j=0;j<G.vexnum;j++)
{
G.arcs[i][j].adj=0;
}
printf("请输入%d条弧的弧尾 弧头:\n",G.arcnum);
for(k=0;k<G.arcnum;++k)
{
scanf("%s%s%*c",&va,&vb);
i=LocateVex(G,va);
j=LocateVex(G,vb);
G.arcs[i][j].adj=1;
}
return OK;
}
int FirstAdjVex(MGraph G,char v)
{ //返回v的第一个邻接顶点的序号。若顶点在G中没有邻接顶点,则返回-1
int i,k;
k=LocateVex(G,v); //k为顶点v在图G中的序号
for(i=0;i<G.vexnum;i++)
if(G.arcs[k][i].adj!=0)
return i;
else return -1;
return OK;
}
int NextAdjVex(MGraph G,char v,char w)
{ //返回v的相对于(w)的下一个邻接顶点的序号,若w是v的最后一个邻接顶点,则返回-1
int i,k1,k2;
k1=LocateVex(G,v); //k1为顶点v在图G中的序号
k2=LocateVex(G,w); //k2为顶点w在图G中的序号
for(i=k2+1;i<G.vexnum;i++)
if(G.arcs[k1][i].adj!=0)
return i;
else return -1;
return OK;
}
int visited[MAX_VERTEX_NUM];
int DFS(MGraph G,int v)
{ //从第v个顶点出发递归的深度优先遍历图G
int w;
visited[v]=TURE; //设置访问标志为TURE
printf("%s",G.vexs[v]);
for(w=FirstAdjVex(G,G.vexs[v]);w>=0;w=NextAdjVex(G,G.vexs[v],G.vexs[w]))
if(!visited[w])
DFS(G,w); //对尚未访问的序号为w的邻接顶点递归的调用DFS
return OK;
}
int DFSTraverse(MGraph G)
{ //从第一个顶点起,深度优先遍历图G
int v;
for(v=0;v<G.vexnum;v++)
visited[v]=FALSE; //访问标志数组初始化
for(v=0;v<G.vexnum;v++)
if(!visited[v])
DFS(G,v); //对尚未访问的顶点v调用DFS
printf("\n");
return OK;
}
int main()
{
MGraph G;
CreateDG(G);
DFSTraverse(G);
system("pause");
return OK;
}
广度可以自己参照改写。。