图的部分操作
6-1 采用邻接表创建无向图 (20 分)
void CreateUDG(ALGraph &G)
{
int n,m;
scanf("%d %d",&n,&m);
G.vexnum = n;
G.arcnum = m;
getchar();
for(int i = 0 ; i < G.vexnum ; i ++){
char s;
scanf("%c",&s);
G.vertices[i].data = s;
G.vertices[i].firstarc = NULL;
}
getchar();
for(int i = 0 ; i < m ; i ++){
char a , b;
scanf("%c%c",&a,&b);
getchar();
for(int j = 0 ; j < n ; j ++){
if(G.vertices[j].data == a){
ArcNode *s = new ArcNode ;
for(int k = 0 ; k < n ; k ++){
if(G.vertices[k].data == b){
s->adjvex = k;
ArcNode *q = new ArcNode;
q->adjvex = j;
q->nextarc = G.vertices[k].firstarc;
G.vertices[k].firstarc = q;
break;
}
}
s->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = s;
break;
}
}
}
}
6-2 采用邻接矩阵表示法创建无向图 (20 分)
void CreateUDN(AMGraph &G)
{
int n,m;
scanf("%d%d",&n,&m);
G.vexnum = n;
G.arcnum = m;
getchar();
for(int i = 0 ; i < n ; i ++){
char s;
scanf("%c",&s);
G.vexs[i] = s;
}
getchar();
for(int i = 0 ; i < m ; i ++){
char a , b;
scanf("%c%c",&a,&b);
getchar();
int x = -1 , y = -1;
for(int j = 0 ; j < n ; j ++){
if(G.vexs[j] == a) x = j;
if(G.vexs[j] == b) y = j;
if(x != -1 && y != -1) break;
}
G.arcs[x][y] = 1;
G.arcs[y][x] = 1;
}
}
6-3 基于邻接矩阵表示的广度优先遍历 (20 分)
#include<queue>
using namespace std;
void BFS(Graph G, int v)
{
visited[v] = 1;
queue<int> q;
q.push(v);
while(!q.empty()){
int x = q.front();
q.pop();
printf("%c ",G.vexs[x]);
for(int i = 0 ; i < G.vexnum ; i ++){
if(!visited[i] && G.arcs[x][i]){
q.push(i);
visited[i] = 1;
}
}
}
}
6-4 邻接矩阵存储图的深度优先遍历 (20 分)
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
{
Visit(V);
Visited[V] = 1;
for(int i = 0 ; i < Graph->Nv ; i ++){
if(Graph->G[V][i] == 1 && !Visited[i]){
DFS(Graph, i, Visit);
}
}
}
6-5 求采用邻接矩阵作为存储结构的有向图各顶点的出度 (6 分)
void outdegree(MGraph G)
{
for(int i = 0 ; i < G.vexnum ; i ++){
printf("%c:",G.vexs[i]);
int sum = 0;
for(int j = 0 ; j < G.vexnum ; j ++) if(G.arcs[i][j]) sum++;
printf("%d\n",sum);
}
}
6-6 基于邻接表表示的广度优先遍历 (20 分)
#include<queue>
using namespace std;
void BFS(ALGraph G, int v){
queue<int> q;
q.push(v);
visited[v] = 1;
while(!q.empty()){
int x = q.front();
printf("%c ",G.vertices[x].data);
q.pop();
ArcNode *s = G.vertices[x].firstarc;
for( ; s != NULL ; s = s->nextarc){
if(!visited[s->adjvex]){
q.push(s->adjvex);
visited[s->adjvex] = 1;
}
}
}
}
6-7 统计有向图中各顶点的入度 (10 分)
void InDegree(LGraph Graph,int *num)
{
for(int i = 0 ; i < Graph->Nv ; i ++){
PtrToAdjVNode s = Graph->G[i].FisrtEdge;
for( ; s != NULL ; s = s->Next){
int x = s->AdjV;
num[x]++;
}
}
}
6-8 邻接表存储图的广度优先遍历 (20 分)
void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
Vertex q[1010],ll = 0 , rr = 0;
Visited[S] = 1;
q[rr ++] = S;
while(rr != ll){
Vertex x = q[ll ++];
Visit(x);
PtrToAdjVNode s = Graph->G[x].FirstEdge;
for(; s != NULL ; s = s->Next){
if(!Visited[s->AdjV]){
Visited[s->AdjV] = 1;
q[rr++] = s->AdjV;
}
}
}
}
6-9 实现基于邻接矩阵表示的深度优先遍历 (20 分)
void DFS(Graph G, int v)
{
visited[v] = 1;
printf("%c ",G.vexs[v]);
for(int i = 0 ; i < G.vexnum ; i ++){
if(!visited[i] && G.arcs[v][i]){
visited[i] = 1;
DFS(G,i);
}
}
}
|