邻接矩阵法
适合边稠密的图
#include <iostream>
#include <cstdlib>
#include <climits>
#include <map>
#define Max 100
#define INFINIT INT_MAX
using namespace std;
struct MGraph
{
int vec[Max];
int edge[Max][Max];
int vec_num;
int edge_num;
};
void creat_DirectMGraph(MGraph &); //有向图
void creat_UndirectMGraph(MGraph &); //无向图
void show(MGraph &); //打印
int main()
{
MGraph G;
int flag = 0;
cout << "构建有向图还是无向图?" << endl;
cout << "1.有向图\t2.无向图" << endl;
cin >> flag;
switch(flag)
{
case 1:
creat_DirectMGraph(G);
show(G);
break;
case 2:
creat_UndirectMGraph(G);
show(G);
break;
default:
cout << "Error!" << endl;
}
system("pause");
return 0;
}
void creat_DirectMGraph(MGraph &G)
{
cout << "请输入顶点数和边数:";
cin >> G.vec_num >> G.edge_num;
cout << "请输入每个顶点的信息:";
for (int i = 0; i < G.vec_num; i++)
cin >> G.vec[i];
//初始化
for (int i = 0; i < G.vec_num; i++)
for (int j = 0; j < G.vec_num; j++)
G.edge[i][j] = INFINIT;
int a, b, value;
for (int i = 0; i < G.edge_num; i++)
{
cout << "请输入第" << i + 1 << "对顶点的邻接关系,以及两顶点间边的权值:";
cin >> a >> b >> value;
G.edge[a - 1][b - 1] = value;
}
}
void creat_UndirectMGraph(MGraph &G)
{
cout << "请输入顶点数和边数:";
cin >> G.vec_num >> G.edge_num;
cout << "请输入每个顶点的信息:";
for (int i = 0; i < G.vec_num; i++)
cin >> G.vec[i];
for (int i = 0; i < G.vec_num; i++)
for (int j = 0; j < G.vec_num; j++)
G.edge[i][j] = INFINIT;
int a, b, value;
for (int i = 0; i < G.edge_num; i++)
{
cout << "请输入第" << i + 1 << "对顶点的邻接关系,以及两顶点间边的权值:";
cin >> a >> b >> value;
G.edge[a - 1][b - 1] = value;
G.edge[b - 1][a - 1] = value;
}
}
void show(MGraph &G)
{
char str[5] = "None";
cout << "您的邻接矩阵:" << endl;
for (int i = 0; i < G.vec_num; i++)
{
for (int j = 0; j < G.vec_num; j++)
{
if (G.edge[i][j] != INFINIT)
printf("%-10d", G.edge[i][j]);
else
printf("%-10s", str);
if (j != G.vec_num - 1)
cout << "\t";
}
cout << endl;
}
}
输出:
构建有向图还是无向图?
1.有向图 2.无向图
1
请输入顶点数和边数:5 6
请输入每个顶点的信息:0 1 2 3 4
请输入第1对顶点的邻接关系,以及两顶点间边的权值:2 1 9
请输入第2对顶点的邻接关系,以及两顶点间边的权值:2 3 3
请输入第3对顶点的邻接关系,以及两顶点间边的权值:3 4 5
请输入第4对顶点的邻接关系,以及两顶点间边的权值:4 5 1
请输入第5对顶点的邻接关系,以及两顶点间边的权值:3 1 2
请输入第6对顶点的邻接关系,以及两顶点间边的权值:1 5 6
您的邻接矩阵:
None None None None 6
9 None 3 None None
2 None None 5 None
None None None None 1
None None None None None
邻接表法
适合边稀疏的图
#include <iostream>
#include <cstdlib>
#define MAX 100
int vnum, rnum;
using namespace std;
struct ArcNode //边表结点
{
int data;
ArcNode* next;
};
struct VNode //顶点表结点
{
int data;
ArcNode* next;
} Adjlist[MAX];
struct ALGraph //顶点表
{
int vexnum;
int arcnum;
};
void creatUndirectALGraph();
void creatDirectALGraph();
void show();
int main()
{
int flag = 0;
cout << "构建有向图还是无向图?" << endl;
cout << "1.有向图\t2.无向图" << endl;
cin >> flag;
switch(flag)
{
case 1:
creatDirectALGraph();
break;
case 2:
creatUndirectALGraph();
break;
default:
cout << "Error!" << endl;
}
show();
system("pause");
return 0;
}
void creatDirectALGraph()
{
cout << "请输入顶点数和边数:";
cin >> vnum >> rnum;
cout << "请输入各结点信息:";
for (int i = 0; i < vnum; i++)
{
cin >> Adjlist[i].data;
Adjlist[i].next = NULL;
}
for (int i = 0; i < rnum; i++)
{
int a = 0;
ArcNode* p = new ArcNode;
p -> next = NULL;
cout << "请输入第" << i + 1 << "对邻接结点:";
cin >> a >> p -> data;
if (Adjlist[a - 1].next == NULL)
Adjlist[a - 1].next = p;
else
{
ArcNode* find = Adjlist[a - 1].next;
while (find -> next != NULL)
find = find -> next;
find -> next = p;
}
p = NULL;
delete p;
}
}
void creatUndirectALGraph()
{
cout << "请输入顶点数和边数:";
cin >> vnum >> rnum;
cout << "请输入各结点信息:";
for (int i = 0; i < vnum; i++)
{
cin >> Adjlist[i].data;
Adjlist[i].next = NULL;
}
for (int i = 0; i < rnum; i++)
{
int a = 0, b = 0;
ArcNode* p1 = new ArcNode;
p1 -> next = NULL;
cout << "请输入第" << i + 1 << "对邻接结点:";
cin >> a >> b;
p1 -> data = b;
if (Adjlist[a - 1].next == NULL)
Adjlist[a - 1].next = p1;
else
{
ArcNode* find = Adjlist[a - 1].next;
while (find -> next != NULL)
find = find -> next;
find -> next = p1;
}
p1 = NULL;
delete p1;
ArcNode* p2 = new ArcNode;
p2 -> next = NULL;
p2 -> data = a;
if (Adjlist[b - 1].next == NULL)
Adjlist[b - 1].next = p2;
else
{
ArcNode* find = Adjlist[b - 1].next;
while (find -> next != NULL)
find = find -> next;
find -> next = p2;
}
p2 = NULL;
delete p2;
}
}
void show()
{
for (int i = 0; i < vnum; i++)
{
cout << Adjlist[i].data << " -> ";
if (Adjlist[i].next == NULL)
{
cout << "NULL" << endl;
continue;
}
else
{
ArcNode* q = Adjlist[i].next;
while (q != NULL)
{
cout << q -> data << " -> ";
q = q -> next;
}
cout << "NULL" << endl;
}
}
}
输出:
构建有向图还是无向图?
1.有向图 2.无向图
2
请输入顶点数和边数:4 5
请输入各结点信息:1 2 3 4
请输入第1对邻接结点:1 2
请输入第2对邻接结点:2 3
请输入第3对邻接结点:3 4
请输入第4对邻接结点:1 4
请输入第5对邻接结点:1 3
1 -> 2 -> 4 -> 3 -> NULL
2 -> 1 -> 3 -> NULL
3 -> 2 -> 4 -> 1 -> NULL
4 -> 3 -> 1 -> NULL