【432】COMP9024,Exercise9
eulerianCycle.c
- What determines whether a graph is Eulerian or not?
- Write a C program that reads a graph, prints the graph, and determines whether an input graph is Eulerian or not.
- if the graph is Eulerian, the program prints an Eulerian path
- you should start with vertex 0
note that you may use the function findEulerianCycle() from the lecture on Graph Search Applications
if it is not Eulerian, the program prints the message Not Eulerian
- if the graph is Eulerian, the program prints an Eulerian path
For example,
- The graph:
#4
0 1 0 2 0 3 1 2 2 3is not Eulerian (can you see why?). Using this as input, your program should output:
V=4, E=5
<0 1> <0 2> <0 3>
<1 0> <1 2>
<2 0> <2 1> <2 3>
<3 0> <3 2>
Not Eulerian In the above-named lecture I showed a 'concentric squares' graph (called concsquares):
#8
0 7 7 5 5 1 1 0
6 0 6 7
2 5 2 7
4 1 4 5
3 0 3 1which is Eulerian, although I've labelled the vertices differently here. For this input your program should produce the output:
V=8, E=12
<0 1> <0 3> <0 6> <0 7>
<1 0> <1 3> <1 4> <1 5>
<2 5> <2 7>
<3 0> <3 1>
<4 1> <4 5>
<5 1> <5 2> <5 4> <5 7>
<6 0> <6 7>
<7 0> <7 2> <7 5> <7 6>
Eulerian cycle: 0 1 4 5 2 7 5 1 3 0 6 7 0Draw concsquares, label it as given in the input file above, and check the cycle is indeed Eulerian.
The function findEulerCycle() in the lecture notes does not handle disconnected graphs. In a disconnected Eulerian graph, each subgraph has an Eulerian cycle.
- Modify this function to handle disconnected graphs.
- With this change, your program should now work for the graph consisting of 2 disconnected triangles:
#6
0 1 0 2 1 2 3 4 3 5 4 5It should now find 2 Eulerian paths:
V=6, E=6
<0 1> <0 2>
<1 0> <1 2>
<2 0> <2 1>
<3 4> <3 5>
<4 3> <4 5>
<5 3> <5 4>
Eulerian cycle: 0 1 2 0
Eulerian cycle: 3 4 5 3
思路:经过一条边就删掉一个,通过遍历查找是否遍历完(针对不连通的graph)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "Graph.h"
#include "Quack.h" #define UNVISITED -1
#define WHITESPACE 100 void dfsR(Graph g, Vertex v, int numV, int *order, int *visited);
Vertex getAdjacent(Graph g, int numV, Vertex v); int readNumV(void) { // returns the number of vertices numV or -1
int numV;
char w[WHITESPACE];
scanf("%[ \t\n]s", w); // skip leading whitespace
if ((getchar() != '#') ||
(scanf("%d", &numV) != 1)) {
fprintf(stderr, "missing number (of vertices)\n");
return -1;
}
return numV;
} int readGraph(int numV, Graph g) { // reads number-number pairs until EOF
int success = true; // returns true if no error
int v1, v2;
while (scanf("%d %d", &v1, &v2) != EOF && success) {
if (v1 < 0 || v1 >= numV || v2 < 0 || v2 >= numV) {
fprintf(stderr, "unable to read edge\n");
success = false;
}
else {
insertE(g, newE(v1, v2));
}
}
return success;
} void findEulerCycle(Graph g, int numV, Vertex startv) {
Quack s = createQuack();
push(startv, s); int allVis = 0;
while (!allVis) {
printf("Eulerian cycle: ");
while (!isEmptyQuack(s)) {
Vertex v = pop(s); // v is the top of stack vertex and ...
push(v, s); // ... the stack has not changed
Vertex w;
if ((w = getAdjacent(g, numV, v)) >= 0) {
push(w, s); // push a neighbour of v onto stack
removeE(g, newE(v, w)); // remove edge to neighbour
}
else {
w = pop(s);
printf("%d ", w);
}
}
printf("\n");
allVis = 1; for (Vertex v = 0; v < numV && allVis; v++) {
for (Vertex w = 0; w < numV && allVis; w++) {
if (isEdge(g, newE(v, w))) {
allVis = 0;
push(v, s);
}
}
}
}
} Vertex getAdjacent(Graph g, int numV, Vertex v) {
// returns the Largest Adjacent Vertex if it exists, else -1
Vertex w;
Vertex lav = -1; // the adjacent vertex
for (w=numV-1; w>=0 && lav==-1; w--) {
Edge e = newE(v, w);
if (isEdge(g, e)) {
lav = w;
}
}
return lav;
} int isEulerian(Graph g, int numV) {
int count = 0;
for (Vertex w = 0; w < numV; w++) {
count = 0;
for (Vertex v = 0; v < numV; v++) {
if (isEdge(g, newE(w, v))) {
count++;
}
}
if (count % 2 != 0) {
return 0;
}
}
return 1;
} int main (void) {
int numV;
if ((numV = readNumV()) >= 0) {
Graph g = newGraph(numV);
if (readGraph(numV, g)) {
showGraph(g); if(isEulerian(g, numV)) {
findEulerCycle(g, numV, 0);
}
else {
printf("Not Eulerian\n");
}
}
}
else {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} // clear && gcc dfs_EulerCycle.c GraphAM.c Quack.c && ./a.out < input_1.txt // clear && gcc dfs_EulerCycle.c GraphAM.c Quack.c && ./a.out < input_2.txt // clear && gcc dfs_EulerCycle.c GraphAM.c Quack.c && ./a.out < input_3.txt
unreachable.c
Write a program that uses a fixed-point computation to find all the vertices in a graph that are unreachable from the start vertex (assume it to be 0). Note the following:
- the fixed-point computation should be iterative
you should not use recursion, or stacks or queues
If a graph is disconnected:
- then those vertices not reachable (say vertices 8 and 9) should be output as follows:
Unreachable vertices = 8 9
If a graph is connected then all vertices are reachable and the output is :
Unreachable vertices = none
For example:
- Here is a graph that consists of 2 disconnected triangles:
#6
0 1 0 2 1 2 3 4 3 5 4 5If the start vertex is 0, then the output should be:
V=6, E=6
<0 1> <0 2>
<1 0> <1 2>
<2 0> <2 1>
<3 4> <3 5>
<4 3> <4 5>
<5 3> <5 4>
Unreachable vertices = 3 4 5because obviously the vertices in the second triangle are not reachable from the first.
- here is a connected graph:
#5
0 1 1 2 2 3 3 4 4 0
1 3 1 4
2 4Starting at any vertex, the result should be:
V=5, E=8
<0 1> <0 4>
<1 0> <1 2> <1 3> <1 4>
<2 1> <2 3> <2 4>
<3 1> <3 2> <3 4>
<4 0> <4 1> <4 2> <4 3>
Unreachable vertices = none
思路:
- 首先就是设置 outside数组,默认是都为 -1,一旦被访问了就赋值为 0,变为 inside
- 设置一个 changing 字符串,用来监测 outside 数组是否有变化
- 如果变化的话,就遍历所有inside的点的相连接的点,如果发现 outside,则将此点赋值为 inside,changing 赋值为1
- while 循环,继续遍历,知道所有 inside 点的邻接点都是 inside,遍历结束
- 因此会将所有一个连通图中的点放入在 inside 内部
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "Graph.h" #define UNVISITED -1
#define WHITESPACE 100 int readNumV(void) { // returns the number of vertices numV or -1
int numV;
char w[WHITESPACE];
scanf("%[ \t\n]s", w); // skip leading whitespace
if ((getchar() != '#') ||
(scanf("%d", &numV) != 1)) {
fprintf(stderr, "missing number (of vertices)\n");
return -1;
}
return numV;
} int readGraph(int numV, Graph g) { // reads number-number pairs until EOF
int success = true; // returns true if no error
int v1, v2;
while (scanf("%d %d", &v1, &v2) != EOF && success) {
if (v1 < 0 || v1 >= numV || v2 < 0 || v2 >= numV) {
fprintf(stderr, "unable to read edge\n");
success = false;
}
else {
insertE(g, newE(v1, v2));
}
}
return success;
} int *mallocArray(int numV) {
int *array = malloc(numV * sizeof(int));// l
if (array == NULL) { // o
fprintf(stderr, "Out of memory\n"); // c
exit(1); // a
} // l
int i; // f
for (i=0; i<numV; i++) { // u
array[i] = UNVISITED; // n
} // c
return array; // t
} void showUnreach(Graph g, int numV, Vertex startv) {
int *outside = mallocArray(numV);
outside[startv] = 0;
int changing = 1;
while (changing) {
changing = 0;
for (Vertex v = 0; v < numV; v++) {
if (!outside[v]) {
for (Vertex w = 0; w < numV; w++) {
if (isEdge(g, newE(v, w)) && outside[w] == UNVISITED) {
outside[w] = 0;
changing = 1;
}
}
}
}
}
printf("Unreachable vertices = ");
int any = 0;
for (Vertex v = 0; v < numV; v++) {
if (outside[v] == UNVISITED) {
printf("%d ", v);
any = 1;
}
}
if (!any) {
printf("none");
}
putchar('\n');
return;
} int main (void) {
int numV;
if ((numV = readNumV()) >= 0) {
Graph g = newGraph(numV);
if (readGraph(numV, g)) {
showGraph(g);
showUnreach(g, numV, 0);
}
}
else {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} // clear && gcc unreachable.c GraphAM.c && ./a.out < input_1.txt // clear && gcc unreachable.c GraphAM.c && ./a.out < input_2.txt // clear && gcc unreachable.c GraphAM.c && ./a.out < input_3.txt
【432】COMP9024,Exercise9的更多相关文章
- 【Demo】QQ,github,微博第三方社交登录
本文主要讲解 集成 第三方社交账号登录 为什么会有这个需求? 主要是因为目前互联网的网站数量太多,如果在各个站点都注册一个账号 用户非常不容易记住每个账号的用户名和密码,并且非常难保证每个账号的密码足 ...
- 【MVC】 js,css 压缩
[MVC] js,css 压缩 一. 引用 System.Web.Optimization.dll : 使用 Nuget ,在控制台输入 Install-Package Microsoft.AspNe ...
- 关于【bootstrap】中,【tooltip】的不算bug的bug的个人看法
先说下遇到这个问题的背景吧. 就是在页面中有个div,这个div右上角(或者其他位置)有个 × 的图标(这个图标添加tooltip工具提示),光标移到这个图标时,触发tooltip,提示“点击移除”这 ...
- Switch选择语句能否作用在String【字符串】上,也就是能否这么写:Switch(一个字符串变量)?
Switch选择语句能否作用在String[字符串]上,也就是能否这么写:Switch(一个字符串变量)? 解答:不可以,只能处理int,byte,short,char,(其实是只能处理int,其它三 ...
- 【多线程】 Task ,async ,await
[多线程]Task ,async ,await 一. WinForm 里经常会用到多线程, 多线程的好出就不多说了,来说说多线程比较麻烦的地方 1. UI 线程与其他线程的同步,主要是 Form 和 ...
- 【题解】Leyni,罗莉和队列(树状数组)
[题解]Leyni,罗莉和队列(树状数组) HRBUST - 1356 将整个序列reverse一下,现在就变成了从高到低的排队.题目就变成了,定位一个妹子,问这个妹子前面的比这个妹子小的妹子中,下标 ...
- 【javascript】您好, 您要的ECMAScript6速记套餐到了
[前言]本文“严重参考” 自阮一峰老师写的文档,在此我郑重感谢他沉默无声的帮助 总结一下ES6为 javascript中的 对象/数组/函数 这JS三巨头所提供的更简洁优雅的书写方式,以及扩展的API ...
- 【转载】通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
本文转载自:http://www.cnblogs.com/1996V/p/9037603.html [尊重作者原创,转载说明出处!感谢作者“小曾看世界”分享! ] 什么是.NET?什么是.NET Fr ...
- 【npm】伙计,给我来一杯package.json!不加糖
前言:夜深了,我熬了一锅热气腾腾的package.json,给大家端上来,希望大家喜欢 json和JS对象的区别 package.json,顾名思义,它是一个json文件,而不能写入JS对象. 所以我 ...
随机推荐
- 样条函数后续(java)--可在hive中执行的函数
之前写的样条插值算法只能在本地执行,但是我想要的是可在hive中执行的jar包,为了符合我的要求,经过痛苦.气愤.悲伤等一系列过程,终于实现了: 想要实现可在hive中执行的jar包,以下是具体步骤: ...
- redis详解(包含使用场景)
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- SVN “Previous operation has not finished”
https://jingyan.baidu.com/article/cbcede0761334902f40b4d31.html 需要运行sqlite3打开.svn下的wc.db数据库文件, sqlit ...
- P1853 投资的最大效益
题目背景 约翰先生获得了一大笔遗产,他暂时还用不上这一笔钱,他决定进行投资以获得更大的效益.银行工作人员向他提供了多种债券,每一种债券都能在固定的投资后,提供稳定的年利息.当然,每一种债券的投资额是不 ...
- IntelliJ IDEA 2017 Dubbo Elastic-job Redis Zookeeper RabbitMQ FastDFS MyCat 简介以及部分实现(三)
前言 首先需要说明一下,与前两章的安装篇不太一样,这篇主要扫清一下这些插件/框架 等都是干什么用的,大多数都会用于服务端或监测工具或其他,作为新手建立一个大概的思想更好的了解自己的项目.废话不多 ...
- UOJ422. 【集训队作业2018】小Z的礼物 [min-max容斥,插头DP]
UOJ 思路 由于没有代码和AC记录的支撑,以下思路可能有错. 看到全部取完,大概可以想到min-max容斥. 由于期望的表达式里面合法方案的个数是在分母里面的,所以可以想到把它记录在状态里. 然而由 ...
- win10系统中对本地端口进行简单分析
突然有事情涉及到本地端口,对相关内容进行了了解,这部分知识应该偏向运维,有些不好理解,查起来也零零散散的,理解的可能也有误……只记录一部分东西 想要查看本地端口的情况,在cmd下使用 netstat ...
- CF1163F Indecisive Taxi Fee(线段树+图论)
做法 这里的修改是暂时的 找到一条最短路径\(E\),需要考虑的是将最短路径上的边增大 每个点考虑与\(1/n\)的最短路径在E上前缀/后缀的位置,设\(L_i,R_i\) 考虑每条边\((u,v)\ ...
- Android中为何使用Log而不用System.out
Android中为何使用Log而不用System.out System.out除了使用方便一点以外有以下缺点:日志不可控制,打印时间无法确定,不能添加过滤器,日志没有级别之分. 使用Log比如想打印一 ...
- 部署Hadoop集群之前的一些系统配置
修改内核参数 在/etc/sysctl.conf文件中添加如下配置(需要root权限)以下参数的默认值是在centos7下查看的fs.file-max = 6815744 //文件描述符总数,默认值: ...