导航:首页 > 源码编译 > 邻接矩阵销毁图的算法

邻接矩阵销毁图的算法

发布时间:2023-03-05 22:34:06

① 求一个数组的邻接矩阵的算法描述

1.先求出第1行和第2行中最大的数6
这个数就是顶点的个数
邻接矩阵即为6阶方阵
2.
构造6阶矩阵,
元素全部赋值0
3.
循环(i=1,...,9)读取每条边的起点和终点,比如第一条边的起点和终点:
1,3
将矩阵第1行第3列的元素赋值为
1.
4.
循环完毕退出.
可显示看看邻接矩阵

② 邻接矩阵和邻接表删除有向图或无向图的一条边的算法。。。急用。。尽量简单些就好。。。。

#include <iostream>

using namespace std;

const int DoctorCount = 7;

struct Constraint
{
int doc1;
int relation;
int doc2;
int days;
};

bool check(int *doctors, Constraint *constrains, int n, int index)
{
if (index < 0 || index > DoctorCount || doctors[index] == -1)
{
return false;
}

for (int i = 0; i <= index; i++)
{
for (int j = 0; j < index; j++)
{
if (doctors[i] == doctors[j] && i != j)
{
return false;
}
}
}

for (int i = 0; i < n; i++)
{
Constraint *p = &constrains[i];
if (p->doc1 <= index && p->doc2 <= index)
{
if (p->relation == 1)
{
if (doctors[p->doc2] - doctors[p->doc1] != p->days)
{
return false;
}
}
else
{
if (doctors[p->doc1] - doctors[p->doc2] != p->days)
{
return false;
}
}
}
}
return true;
}

bool slove(int *doctors, Constraint *constrains, int n, int index)
{
if (index >= DoctorCount)
{
return true;
}

if (doctors[index] != -1)
{
return slove(doctors, constrains, n, index + 1);
}

for (int i = 1; i <= 7; i++)
{
doctors[index] = i;
if (check(doctors, constrains, n, index))
{
if (slove(doctors, constrains, n, index + 1))
{
return true;
}
}
doctors[index] = -1;
}

return false;
}

int main( )
{
int n;
cin>>n;

int doctors[7];
for (int i = 0; i < 7; i++)
{
doctors[i] = -1;
}
Constraint *constraints;
constraints = new Constraint[n];

int constraints_count = 0;
for (int i = 0; i < n; i++)
{
char tmp[20];
cin>>tmp;
if (tmp[1] == '=')
{
doctors[tmp[0] - 'A'] = tmp[2] - '0';
}
else
{
constraints[i].doc1 = tmp[0] - 'A';
constraints[i].relation = tmp[1] == '>' ? 1 : 0;
constraints[i].doc2 = tmp[2] - 'A';
constraints[i].days = tmp[3] - '0';
constraints_count++;
}
}

slove(doctors, constraints, constraints_count, 0);
for (int i = 0; i < 7; i++)
{
cout<<(char)(i + 'A')<<" "<<doctors[i]<<endl;
}
delete []constraints;
return 0;
}

③ 存储结构为邻接矩阵,怎么编写无向图添加、删除一个顶点,添加、删除一条边的算法

  1. 邻接表,存储方法跟树的孩子链表示法相类似,是一种顺序分配和链式分配相结合的存储结构。如这个表头结点所对应的顶点存在相邻顶点,则把相邻顶点依次存放于表头结点所指向的单向链表中。

  2. 对于无向图来说,使用邻接表进行存储也会出现数据冗余,表头结点A所指链表中存在一个指向C的表结点的同时,表头结点C所指链表也会存在一个指向A的表结点。

  3. /* MGraph.cc: 图的邻接矩阵存储表示和实现 */

  4. /* 包含图类型Graph定义;创建图;深度优先遍历;广度优先遍历 */

  5. /* 用到引用型参数,在TC下无法通过编译,VC等C++编译器可通过 */

  6. #include <stdio.h>

  7. #include <string.h>

  8. #include <limits.h> //含INT_MAX

  9. #define VType char //顶点值类型

  10. #define EType int //边权值类型

  11. #define MAXVNUM 50 //最大顶点个数

  12. #define DIGRAPH 0 //有向图(网)

  13. #define UNDIGRAPH 1 //无向图(网)

  14. #define INVALID INT_MAX //无效权值(最大整数表示无穷大)

  15. #define EMPTY -1 //"空"顶点序号

④ 求解邻接表建图的算法

这个都好说,不过你要用邻借表保存图,还是已知邻接表打印图?这两个是不同的算法。

阅读全文

与邻接矩阵销毁图的算法相关的资料

热点内容
android手机安装失败 浏览:28
云计算没有服务器 浏览:67
怎么显示android的APP 浏览:121
c编译器怎么删除空格 浏览:695
php自动释放内存 浏览:219
golang编译库 浏览:794
oracle数据字符串加密 浏览:603
研究生去上海当程序员 浏览:90
u8电脑服务器连接失败怎么解决 浏览:569
bat脚本创建日期命名文件夹 浏览:104
将图片转换为pdf格式 浏览:980
java中形参 浏览:83
枚举类型编译器 浏览:519
oraclejava包 浏览:568
手机定位手机怎么定位安卓 浏览:523
在哪个app买欧莱雅最便宜 浏览:495
程序员吃零食好吗 浏览:261
php工程师主要做什么 浏览:356
tvp保存到哪个文件夹 浏览:197
怎么把空调里面的压缩机拆卸掉 浏览:943