#include<stdbool.h>typedefstruct GraphRep *Graph;// vertices are intstypedefint Vertex;// edges are pairs of vertices (end-points)typedefstruct Edge
{
Vertex v;
Vertex w;} Edge;
Graph newGraph(int);voidinsertEdge(Graph, Edge);voidremoveEdge(Graph, Edge);
bool adjacent(Graph, Vertex, Vertex);voidshowGraph(Graph);voidfreeGraph(Graph);
The complete Graph.c source file
#include"Graph.h"#include<assert.h>#include<stdio.h>#include<stdlib.h>typedefstruct GraphRep
{
int**edges;// adjacency matrixint nV;// #verticesint nE;// #edges} GraphRep;
Graph newGraph(int V){
assert(V >=0);int i;
Graph g =malloc(sizeof(GraphRep));assert(g !=NULL);
g->nV = V;
g->nE =0;// allocate memory for each row
g->edges =malloc(V *sizeof(int*));assert(g->edges !=NULL);// allocate memory for each column and initialise with 0for(i =0; i < V; i++){
g->edges[i]=calloc(V,sizeof(int));assert(g->edges[i]!=NULL);}return g;}// check if vertex is valid in a graph
bool validV(Graph g, Vertex v){
return(g !=NULL&& v >=0&& v < g->nV);}voidinsertEdge(Graph g, Edge e){
assert(g !=NULL&&validV(g, e.v)&&validV(g, e.w));if(!g->edges[e.v][e.w]){
// edge e not in graph
g->edges[e.v][e.w]=1;
g->edges[e.w][e.v]=1;
g->nE++;}}voidremoveEdge(Graph g, Edge e){
assert(g !=NULL&&validV(g, e.v)&&validV(g, e.w));if(g->edges[e.v][e.w]){
// edge e in graph
g->edges[e.v][e.w]=0;
g->edges[e.w][e.v]=0;
g->nE--;}}
bool adjacent(Graph g, Vertex v, Vertex w){
assert(g !=NULL&&validV(g, v)&&validV(g, w));return(g->edges[v][w]!=0);}voidshowGraph(Graph g){
assert(g !=NULL);int i, j;printf("Number of vertices: %d\n", g->nV);printf("Number of edges: %d\n", g->nE);for(i =0; i < g->nV; i++)for(j = i +1; j < g->nV; j++)if(g->edges[i][j])printf("Edge %d - %d\n", i, j);}voidfreeGraph(Graph g){
assert(g !=NULL);int i;for(i =0; i < g->nV; i++)free(g->edges[i]);free(g->edges);free(g);}
Complete euler.c source file (main test function)
#include<stdbool.h>#include<stdio.h>#include"Graph.h"// Return the number of vertices that v is// connected tointdegree(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;}// If start from vertex v, decide if the// graph has euler path
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;}elseif(degree(g, nV, v)%2!=0){
return false;}
Vertex x;for(x =0; x < nV; x++)if(x != v && x != w &°ree(g, nV, x)%2!=0)return false;return true;}intmain(void){
Edge e;int n;printf("Enter the number of vertices: ");scanf("%d",&n);
Graph g =newGraph(n);
Vertex src, dest;printf("Enter source node: ");scanf("%d",&src);printf("Enter destination node: ");scanf("%d",&dest);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");printf("The graph has ");if(hasEulerPath(g, n, src, dest))printf("an");elseprintf("no");printf(" Euler path from %d to %d.\n", src, dest);freeGraph(g);return0;}