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

邻接矩阵销毁图的算法

发布时间: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 //"空"顶点序号

④ 求解邻接表建图的算法

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

阅读全文

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

热点内容
月收益翻倍的源码 浏览:632
asop源码放在哪里 浏览:985
电脑服务器密码怎么找 浏览:572
jdp转换pdf 浏览:744
把pdf导入iphone 浏览:508
米哈游租赁的云服务器是哪个 浏览:524
android直接打电话 浏览:1016
ubuntu停止命令 浏览:283
cnc攻丝编程 浏览:869
换个手机号码app怎么注册 浏览:319
怎么下载小猴口算app 浏览:115
轻链app的货怎么样 浏览:625
电脑里的u盘如何加密 浏览:370
我的世界全部版本服务器下载地址 浏览:50
交换原理pdf 浏览:228
菜鸟驿站app怎么邀请新人 浏览:448
电脑里总是有一些1k的文件夹 浏览:45
drm加密绝对安全 浏览:512
android灭屏流程 浏览:496
如何更改站点文件夹名字 浏览:896