2020-01-17 12:10:31 +03:00
|
|
|
#include "Graph.h"
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// Return the number of vertices that v is
|
2020-01-17 12:10:31 +03:00
|
|
|
// connected to
|
2020-05-29 23:23:24 +03:00
|
|
|
int degree(Graph g, int nV, Vertex v)
|
|
|
|
{
|
|
|
|
int deg = 0;
|
|
|
|
Vertex w;
|
|
|
|
for (w = 0; w < nV; w++)
|
|
|
|
if (adjacent(g, v, w))
|
|
|
|
deg++;
|
|
|
|
return deg;
|
2020-01-17 12:10:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// If start from vertex v, decide if the
|
2020-01-17 12:10:31 +03:00
|
|
|
// graph has euler path
|
2020-05-29 23:23:24 +03:00
|
|
|
bool hasEulerPath(Graph g, int nV, Vertex v, Vertex w)
|
|
|
|
{
|
|
|
|
if (v != w)
|
|
|
|
{
|
|
|
|
if (degree(g, nV, v) % 2 == 0 || degree(g, nV, w) % 2 == 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (degree(g, nV, v) % 2 != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Vertex x;
|
|
|
|
for (x = 0; x < nV; x++)
|
|
|
|
if (x != v && x != w && degree(g, nV, x) % 2 != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
2020-01-17 12:10:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
Edge e;
|
|
|
|
int n;
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Enter the number of vertices: ");
|
|
|
|
scanf("%d", &n);
|
|
|
|
Graph g = newGraph(n);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
Vertex src, dest;
|
|
|
|
printf("Enter source node: ");
|
|
|
|
scanf("%d", &src);
|
|
|
|
printf("Enter destination node: ");
|
|
|
|
scanf("%d", &dest);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Enter an edge (from): ");
|
|
|
|
while (scanf("%d", &e.v) == 1)
|
|
|
|
{
|
|
|
|
printf("Enter an edge (to): ");
|
|
|
|
scanf("%d", &e.w);
|
|
|
|
insertEdge(g, e);
|
|
|
|
printf("Enter an edge (from): ");
|
|
|
|
}
|
|
|
|
printf("Finished.\n");
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("The graph has ");
|
|
|
|
if (hasEulerPath(g, n, src, dest))
|
|
|
|
printf("an");
|
|
|
|
else
|
|
|
|
printf("no");
|
|
|
|
printf(" Euler path from %d to %d.\n", src, dest);
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
freeGraph(g);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-17 12:10:31 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// By
|
|
|
|
// .----------------. .----------------. .----------------.
|
|
|
|
// .-----------------. .----------------. .----------------.
|
|
|
|
// | .--------------. || .--------------. || .--------------. ||
|
|
|
|
// .--------------. | | .--------------. || .--------------. | | | _________ |
|
|
|
|
// || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____
|
|
|
|
// | || | ____ | | | | | _ _ | | || ||_ _||_ _|| || | / \
|
|
|
|
// | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | | | | |_/ | |
|
|
|
|
// \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | |
|
|
|
|
// |__| | | || | / .--. \ | | | | | | | || | | ' ' | | || |
|
|
|
|
// / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
|
|
|
|
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_
|
|
|
|
// | | | | _| | | |_ | || | \ `--' / | | | | |_____| | || | `.__.'
|
|
|
|
// | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.'
|
|
|
|
// | | | | | || | | || | | || | | | | |
|
|
|
|
// | || | | | | '--------------' || '--------------' ||
|
|
|
|
// '--------------' || '--------------' | | '--------------' || '--------------'
|
|
|
|
// |
|
|
|
|
// '----------------' '----------------' '----------------'
|
|
|
|
// '----------------' '----------------' '----------------'
|
2020-01-17 12:10:31 +03:00
|
|
|
|
|
|
|
// Email : z5261243@unsw.edu.au
|
|
|
|
// hhoanhtuann@gmail.com
|