eulerianCycle.c

  1. What determines whether a graph is Eulerian or not?
  2. 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

For example,

  • The graph:

    1. #4
    2. 0 1 0 2 0 3 1 2 2 3

    is not Eulerian (can you see why?). Using this as input, your program should output:

    1. V=4, E=5
    2. <0 1> <0 2> <0 3>
    3. <1 0> <1 2>
    4. <2 0> <2 1> <2 3>
    5. <3 0> <3 2>
    6. Not Eulerian
  • In the above-named lecture I showed a 'concentric squares' graph (called concsquares):

    1. #8
    2. 0 7 7 5 5 1 1 0
    3. 6 0 6 7
    4. 2 5 2 7
    5. 4 1 4 5
    6. 3 0 3 1

    which is Eulerian, although I've labelled the vertices differently here. For this input your program should produce the output:

    1. V=8, E=12
    2. <0 1> <0 3> <0 6> <0 7>
    3. <1 0> <1 3> <1 4> <1 5>
    4. <2 5> <2 7>
    5. <3 0> <3 1>
    6. <4 1> <4 5>
    7. <5 1> <5 2> <5 4> <5 7>
    8. <6 0> <6 7>
    9. <7 0> <7 2> <7 5> <7 6>
    10. Eulerian cycle: 0 1 4 5 2 7 5 1 3 0 6 7 0

    Draw 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:
      1. #6
      2. 0 1 0 2 1 2 3 4 3 5 4 5

      It should now find 2 Eulerian paths:

      1. V=6, E=6
      2. <0 1> <0 2>
      3. <1 0> <1 2>
      4. <2 0> <2 1>
      5. <3 4> <3 5>
      6. <4 3> <4 5>
      7. <5 3> <5 4>
      8. Eulerian cycle: 0 1 2 0
      9. Eulerian cycle: 3 4 5 3

思路:经过一条边就删掉一个,通过遍历查找是否遍历完(针对不连通的graph)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include "Graph.h"
  5. #include "Quack.h"
  6.  
  7. #define UNVISITED -1
  8. #define WHITESPACE 100
  9.  
  10. void dfsR(Graph g, Vertex v, int numV, int *order, int *visited);
  11. Vertex getAdjacent(Graph g, int numV, Vertex v);
  12.  
  13. int readNumV(void) { // returns the number of vertices numV or -1
  14. int numV;
  15. char w[WHITESPACE];
  16. scanf("%[ \t\n]s", w); // skip leading whitespace
  17. if ((getchar() != '#') ||
  18. (scanf("%d", &numV) != 1)) {
  19. fprintf(stderr, "missing number (of vertices)\n");
  20. return -1;
  21. }
  22. return numV;
  23. }
  24.  
  25. int readGraph(int numV, Graph g) { // reads number-number pairs until EOF
  26. int success = true; // returns true if no error
  27. int v1, v2;
  28. while (scanf("%d %d", &v1, &v2) != EOF && success) {
  29. if (v1 < 0 || v1 >= numV || v2 < 0 || v2 >= numV) {
  30. fprintf(stderr, "unable to read edge\n");
  31. success = false;
  32. }
  33. else {
  34. insertE(g, newE(v1, v2));
  35. }
  36. }
  37. return success;
  38. }
  39.  
  40. void findEulerCycle(Graph g, int numV, Vertex startv) {
  41. Quack s = createQuack();
  42. push(startv, s);
  43.  
  44. int allVis = 0;
  45. while (!allVis) {
  46. printf("Eulerian cycle: ");
  47. while (!isEmptyQuack(s)) {
  48. Vertex v = pop(s); // v is the top of stack vertex and ...
  49. push(v, s); // ... the stack has not changed
  50. Vertex w;
  51. if ((w = getAdjacent(g, numV, v)) >= 0) {
  52. push(w, s); // push a neighbour of v onto stack
  53. removeE(g, newE(v, w)); // remove edge to neighbour
  54. }
  55. else {
  56. w = pop(s);
  57. printf("%d ", w);
  58. }
  59. }
  60. printf("\n");
  61. allVis = 1;
  62.  
  63. for (Vertex v = 0; v < numV && allVis; v++) {
  64. for (Vertex w = 0; w < numV && allVis; w++) {
  65. if (isEdge(g, newE(v, w))) {
  66. allVis = 0;
  67. push(v, s);
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. Vertex getAdjacent(Graph g, int numV, Vertex v) {
  75. // returns the Largest Adjacent Vertex if it exists, else -1
  76. Vertex w;
  77. Vertex lav = -1; // the adjacent vertex
  78. for (w=numV-1; w>=0 && lav==-1; w--) {
  79. Edge e = newE(v, w);
  80. if (isEdge(g, e)) {
  81. lav = w;
  82. }
  83. }
  84. return lav;
  85. }
  86.  
  87. int isEulerian(Graph g, int numV) {
  88. int count = 0;
  89. for (Vertex w = 0; w < numV; w++) {
  90. count = 0;
  91. for (Vertex v = 0; v < numV; v++) {
  92. if (isEdge(g, newE(w, v))) {
  93. count++;
  94. }
  95. }
  96. if (count % 2 != 0) {
  97. return 0;
  98. }
  99. }
  100. return 1;
  101. }
  102.  
  103. int main (void) {
  104. int numV;
  105. if ((numV = readNumV()) >= 0) {
  106. Graph g = newGraph(numV);
  107. if (readGraph(numV, g)) {
  108. showGraph(g);
  109.  
  110. if(isEulerian(g, numV)) {
  111. findEulerCycle(g, numV, 0);
  112. }
  113. else {
  114. printf("Not Eulerian\n");
  115. }
  116. }
  117. }
  118. else {
  119. return EXIT_FAILURE;
  120. }
  121. return EXIT_SUCCESS;
  122. }
  123.  
  124. // clear && gcc dfs_EulerCycle.c GraphAM.c Quack.c && ./a.out < input_1.txt
  125.  
  126. // clear && gcc dfs_EulerCycle.c GraphAM.c Quack.c && ./a.out < input_2.txt
  127.  
  128. // 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:

    1. Unreachable vertices = 8 9

If a graph is connected then all vertices are reachable and the output is :

    1. Unreachable vertices = none

For example:

  • Here is a graph that consists of 2 disconnected triangles:

    1. #6
    2. 0 1 0 2 1 2 3 4 3 5 4 5

    If the start vertex is 0, then the output should be:

    1. V=6, E=6
    2. <0 1> <0 2>
    3. <1 0> <1 2>
    4. <2 0> <2 1>
    5. <3 4> <3 5>
    6. <4 3> <4 5>
    7. <5 3> <5 4>
    8. Unreachable vertices = 3 4 5

    because obviously the vertices in the second triangle are not reachable from the first.

  • here is a connected graph:
    1. #5
    2. 0 1 1 2 2 3 3 4 4 0
    3. 1 3 1 4
    4. 2 4

    Starting at any vertex, the result should be:

    1. V=5, E=8
    2. <0 1> <0 4>
    3. <1 0> <1 2> <1 3> <1 4>
    4. <2 1> <2 3> <2 4>
    5. <3 1> <3 2> <3 4>
    6. <4 0> <4 1> <4 2> <4 3>
    7. Unreachable vertices = none

思路:

  • 首先就是设置 outside数组,默认是都为 -1,一旦被访问了就赋值为 0,变为 inside
  • 设置一个 changing 字符串,用来监测 outside 数组是否有变化
  • 如果变化的话,就遍历所有inside的点的相连接的点,如果发现 outside,则将此点赋值为 inside,changing 赋值为1
  • while 循环,继续遍历,知道所有 inside 点的邻接点都是 inside,遍历结束
  • 因此会将所有一个连通图中的点放入在 inside 内部
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include "Graph.h"
  5.  
  6. #define UNVISITED -1
  7. #define WHITESPACE 100
  8.  
  9. int readNumV(void) { // returns the number of vertices numV or -1
  10. int numV;
  11. char w[WHITESPACE];
  12. scanf("%[ \t\n]s", w); // skip leading whitespace
  13. if ((getchar() != '#') ||
  14. (scanf("%d", &numV) != 1)) {
  15. fprintf(stderr, "missing number (of vertices)\n");
  16. return -1;
  17. }
  18. return numV;
  19. }
  20.  
  21. int readGraph(int numV, Graph g) { // reads number-number pairs until EOF
  22. int success = true; // returns true if no error
  23. int v1, v2;
  24. while (scanf("%d %d", &v1, &v2) != EOF && success) {
  25. if (v1 < 0 || v1 >= numV || v2 < 0 || v2 >= numV) {
  26. fprintf(stderr, "unable to read edge\n");
  27. success = false;
  28. }
  29. else {
  30. insertE(g, newE(v1, v2));
  31. }
  32. }
  33. return success;
  34. }
  35.  
  36. int *mallocArray(int numV) {
  37. int *array = malloc(numV * sizeof(int));// l
  38. if (array == NULL) { // o
  39. fprintf(stderr, "Out of memory\n"); // c
  40. exit(1); // a
  41. } // l
  42. int i; // f
  43. for (i=0; i<numV; i++) { // u
  44. array[i] = UNVISITED; // n
  45. } // c
  46. return array; // t
  47. }
  48.  
  49. void showUnreach(Graph g, int numV, Vertex startv) {
  50. int *outside = mallocArray(numV);
  51. outside[startv] = 0;
  52. int changing = 1;
  53. while (changing) {
  54. changing = 0;
  55. for (Vertex v = 0; v < numV; v++) {
  56. if (!outside[v]) {
  57. for (Vertex w = 0; w < numV; w++) {
  58. if (isEdge(g, newE(v, w)) && outside[w] == UNVISITED) {
  59. outside[w] = 0;
  60. changing = 1;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. printf("Unreachable vertices = ");
  67. int any = 0;
  68. for (Vertex v = 0; v < numV; v++) {
  69. if (outside[v] == UNVISITED) {
  70. printf("%d ", v);
  71. any = 1;
  72. }
  73. }
  74. if (!any) {
  75. printf("none");
  76. }
  77. putchar('\n');
  78. return;
  79. }
  80.  
  81. int main (void) {
  82. int numV;
  83. if ((numV = readNumV()) >= 0) {
  84. Graph g = newGraph(numV);
  85. if (readGraph(numV, g)) {
  86. showGraph(g);
  87. showUnreach(g, numV, 0);
  88. }
  89. }
  90. else {
  91. return EXIT_FAILURE;
  92. }
  93. return EXIT_SUCCESS;
  94. }
  95.  
  96. // clear && gcc unreachable.c GraphAM.c && ./a.out < input_1.txt
  97.  
  98. // clear && gcc unreachable.c GraphAM.c && ./a.out < input_2.txt
  99.  
  100. // clear && gcc unreachable.c GraphAM.c && ./a.out < input_3.txt

【432】COMP9024,Exercise9的更多相关文章

  1. 【Demo】QQ,github,微博第三方社交登录

    本文主要讲解 集成 第三方社交账号登录 为什么会有这个需求? 主要是因为目前互联网的网站数量太多,如果在各个站点都注册一个账号 用户非常不容易记住每个账号的用户名和密码,并且非常难保证每个账号的密码足 ...

  2. 【MVC】 js,css 压缩

    [MVC] js,css 压缩 一. 引用 System.Web.Optimization.dll : 使用 Nuget ,在控制台输入 Install-Package Microsoft.AspNe ...

  3. 关于【bootstrap】中,【tooltip】的不算bug的bug的个人看法

    先说下遇到这个问题的背景吧. 就是在页面中有个div,这个div右上角(或者其他位置)有个 × 的图标(这个图标添加tooltip工具提示),光标移到这个图标时,触发tooltip,提示“点击移除”这 ...

  4. Switch选择语句能否作用在String【字符串】上,也就是能否这么写:Switch(一个字符串变量)?

    Switch选择语句能否作用在String[字符串]上,也就是能否这么写:Switch(一个字符串变量)? 解答:不可以,只能处理int,byte,short,char,(其实是只能处理int,其它三 ...

  5. 【多线程】 Task ,async ,await

    [多线程]Task ,async ,await 一. WinForm 里经常会用到多线程, 多线程的好出就不多说了,来说说多线程比较麻烦的地方 1. UI 线程与其他线程的同步,主要是 Form 和 ...

  6. 【题解】Leyni,罗莉和队列(树状数组)

    [题解]Leyni,罗莉和队列(树状数组) HRBUST - 1356 将整个序列reverse一下,现在就变成了从高到低的排队.题目就变成了,定位一个妹子,问这个妹子前面的比这个妹子小的妹子中,下标 ...

  7. 【javascript】您好, 您要的ECMAScript6速记套餐到了

    [前言]本文“严重参考” 自阮一峰老师写的文档,在此我郑重感谢他沉默无声的帮助 总结一下ES6为 javascript中的 对象/数组/函数 这JS三巨头所提供的更简洁优雅的书写方式,以及扩展的API ...

  8. 【转载】通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?

    本文转载自:http://www.cnblogs.com/1996V/p/9037603.html [尊重作者原创,转载说明出处!感谢作者“小曾看世界”分享! ] 什么是.NET?什么是.NET Fr ...

  9. 【npm】伙计,给我来一杯package.json!不加糖

    前言:夜深了,我熬了一锅热气腾腾的package.json,给大家端上来,希望大家喜欢 json和JS对象的区别 package.json,顾名思义,它是一个json文件,而不能写入JS对象. 所以我 ...

随机推荐

  1. new char()与new char[]区别

    char *pc = new char(15); //开辟一个内存单元,并用括号里的初始化(用15来初始化你定义的指针所指向的那个char)char *pc = new char[15]; //开辟一 ...

  2. java web 向数据库插入中文数据乱码问题

    一.先检查下是 页面返回数据时已经乱码了,还是在插入数据库的时候乱的码. 二.页面返回乱码: 1.  Web.XML  文件配置 <!-- 配置编码过滤器 --> <filter&g ...

  3. JS AJAX和JSONP的基础功能封装以及使用示例;

    1.代码: function ajax(options){ options = options || {}; options.type = options.type || "get" ...

  4. Mysql 为什么不建议在 MySQL 中使用 UTF-8?

    最近我遇到了一个bug,我试着通过Rails在以“utf8”编码的MariaDB中保存一个UTF-8字符串,然后出现了一个离奇的错误: Incorrect string value: ‘😃 &l ...

  5. Cogs 56. 质数取石子(博弈)

    质数取石子 ★★ 输入文件:stonegame.in 输出文件:stonegame.out 简单对比 时间限制:1 s 内存限制:128 MB 问题描述 DD 和 MM 正在玩取石子游戏.他们的游戏规 ...

  6. [Shell]利用JS文件反弹Shell

    0x01 模拟环境 攻击: kali ip: 192.168.248.132 测试: windows 7 x64 ip: 192.168.248.136 0x02 工具地址 https://githu ...

  7. OpenFOAM-圆柱绕流

    原版视频下载地址:https://yunpan.cn/c64yrdt9J5LmQ  访问密码 0128 首先进行建模操作,任何建模软件均可,本教程采用ICEM直接建模,模型尺寸如下: 建成的模型如下: ...

  8. CSS工作记录

    1:行内元素 设置背景图片(假设 给span) /*span 标签加背景图片 需要设置快级元素 定义高度宽度,当高度宽度很小的时候 需要设置背景图片大小*/ .filex { display: inl ...

  9. 用sublime3编写运行16位汇编程序_详细教程

    最近需要学8086汇编,课堂教学竟然是PPT看代码,然而不运行程序是没法学编程的.网上的教程有很多坑点,摸索出了正确的步骤. 1.安装sublime3.安装MASM32.64位系统安装DOSBOX(因 ...

  10. #C++初学记录(判断子串#数学结合)

    A Count Task Problem Description Count is one of WNJXYK's favorite tasks. Recently, he had a very lo ...