[英]How do I implement these graph functions below to make sure I'm covering all test cases with the given constraints?
I've recently gotten into discrete math and graphs.我最近开始研究离散数学和图表。 I'm trying to implement a few functions related to graphs.
我正在尝试实现一些与图形相关的功能。
But I'm not sure if I'm covering every test case that could possibly be thrown at my program.但我不确定我是否涵盖了每个可能被扔到我的程序中的测试用例。
How to cover all cases?如何覆盖所有情况?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "graphprofile.h"
int main(int argc, char const *argv[]) {
int tc, n, i, j, ans, ret;
clock_t t;
int **g;
scanf("%d", &tc);
while(tc--) {
t=clock();
printf("New testcase begin...\n");
scanf("%d", &n);
g = (int**) malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
g[i] = (int*) malloc(n * sizeof(int));
for (j = 0; j < n; j++) {
scanf("%d", &g[i][j]);
}
}
//AvgDegreeTest
scanf("%d", &ans);
ret = avgDegree(g, n);
if(ret == ans) {
printf("AvgDegreeTest passed.\n");
} else {
printf("AvgDegreeTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//IsRegularTest
scanf("%d", &ans);
ret = isRegular(g, n);
if(ret == ans) {
printf("IsRegularTest passed.\n");
} else {
printf("IsRegularTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//IsCompleteTest
scanf("%d", &ans);
ret = isComplete(g, n);
if(ret == ans) {
printf("IsCompleteTest passed.\n");
} else {
printf("IsCompleteTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//IsCycleTest
scanf("%d", &ans);
ret = isCycleGraph(g, n);
if(ret == ans) {
printf("IsCycleTest passed.\n");
} else {
printf("IsCycleTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//IsPathTest
scanf("%d", &ans);
ret = isPathGraph(g, n);
if(ret == ans) {
printf("IsPathTest passed.\n");
} else {
printf("IsPathTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//HasEulerCktTest
scanf("%d", &ans);
ret = hasEulerCkt(g, n);
if(ret == ans) {
printf("HasEulerCktTest passed.\n");
} else {
printf("HasEulerCktTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//HasEulerPathTest
scanf("%d", &ans);
ret = hasEulerPath(g, n);
if(ret == ans) {
printf("HasEulerPathTest passed.\n");
} else {
printf("HasEulerPathTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
//OresTheoremTest
scanf("%d", &ans);
ret = satifiesOresTheorem(g, n);
if(ret == ans) {
printf("OresTheoremTest passed.\n");
} else {
printf("OresTheoremTest ***FAILED***! ");
printf("(Expected: %d, Returned: %d)\n", ans, ret);
}
for (i = 0; i < n; i++) {
free(g[i]);
}
free(g);
t= clock() -t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("Testcase %d took %f seconds to execute \n",tc,time_taken);
printf("\n%d testcase left.\n\n", tc);
}
return 0;
}
// 1. What is average degree of a vertex in the graph?
int avgDegree(int **g, int n);
// 2. Is the graph a regular graph?
int isRegular(int **g, int n);
// 3. Is the graph a complete graph?
int isComplete(int **g, int n);
// 4. Is the graph a cycle graph?
int isCycleGraph(int **g, int n);
// 5. Is the graph a path graph but not a cycle graph?
int isPathGraph(int **g, int n);
// 6. Does the graph has an Euler circuit?
int hasEulerCkt(int **g, int n);
// 7. Does the graph has an Euler path but not an Euler circuit?
int hasEulerPath(int **g, int n);
// 8. Does the graph satisfy the sufficient condition of the Ore's theorem?
// Sufficient condition for the graph to have a Hamilton according the Ore's theorem:
// deg(u) + deg(v) ≥ n for every pair of nonadjacent vertices u and v in the graph
int satifiesOresTheorem(int **g, int n);
#include <stdlib.h>
#include "graphprofile.h"
// Returns list of degrees of each vertex of the graph
int* degreeCal(int **g,int n)
{ int* degList=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{ degList[i]=0;
for(int j=0;j<n;j++)
if(g[i][j]==1)
degList[i]++;
}
return degList;
}
int degSum(int** g,int n)
{
int* degList = degreeCal(g,n);
int sum=0;
for(int i=0;i<n;i++)
sum+=degList[i];
return sum;
}
// 1. What is average degree of a vertex in the graph?
int avgDegree(int **g, int n) {
return (degSum(g,n))/n;
}
// 2. Is the graph a regular graph?
int isRegular(int **g, int n) {
int* degList;
degList = degreeCal(g,n);
int cd = degList[0];
for(int i=1;i<n;i++)
{
if(cd!=degList[i])
return 0;
}
return 1;
}
// Returns list of degrees of each vertex of the graph
// 3. Is the graph a complete graph?
int isComplete(int **g, int n) {
int sum = degSum(g,n);
int e = sum/2;
if((n*(n-1)/2)==e)
return 1;
else
{
return 0;
}
}
// 4. Is the graph a cycle graph?
int isCycleGraph(int **g, int n) {
int* degList = degreeCal(g,n);
int e = degSum(g,n)/2;
for(int i=0;i<n;i++)
{
if(degList[i]!=2)
return 0;
}
if(e==n)
return 1;
else
{
return 0;
}
}
// 5. Is the graph a path graph but not a cycle graph?
int isPathGraph(int **g, int n) {
int* degList = degreeCal(g,n);
int one = 0;
int e = degSum(g,n)/2;
for(int i=0;i<n;i++)
{
if(degList[i]==2)
continue;
else if(degList[i]==1 && one<=2)
one++;
else
return 0;
}
if(one==2 && e==n-1)
return 1;
else
{
return 0;
}
}
// 6. Does the graph has an Euler circuit?
int hasEulerCkt(int **g, int n) {
int* degList = degreeCal(g,n);
for(int i=0;i<n;i++)
{if(degList[i]%2!=0)
return 0;
}
return 1;
}
// 7. Does the graph has an Euler path but not an Euler circuit?
int hasEulerPath(int **g, int n) {
int* degList = degreeCal(g,n);
int sum_o = 0;
for(int i=0;i<n;i++)
{
if(degList[i]%2!=0)
sum_o++;
}
if(sum_o==2)
return 1;
else
return 0;
}
// 8. Does the graph satisfy the sufficient condition of the Ore's theorem?
// Sufficient condition for the graph to have a Hamilton according the Ore's theorem:
// deg(u) + deg(v) ≥ n for every pair of nonadjacent vertices u and v in the graph
int satifiesOresTheorem(int **g, int n) {
int* degList = degreeCal(g,n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j && g[i][j]==0)
if(degList[i]+degList[j]<n)
return 0;
return 1;
}
2
4
0 0 1 1
0 0 1 1
1 1 0 0
1 1 0 0
2 1 0 1 0 1 0 1
3
0 1 1
1 0 0
1 0 0
1 0 0 0 1 0 1 0
Anyway, I realised that the definitions of the types of graphs themselves prove to be enough for me to implement them in C.无论如何,我意识到图形类型本身的定义足以让我在 C 中实现它们。
//Profile a connected undirected unweighted graph for the following properties.
#include <stdlib.h>
#include "graphprofile.h"
// 1. What is average degree of a vertex in the graph?
int avgDegree(int **g, int n)
{
int e = 0;
int result;
for(int i = 0; i<n; i++)
{
for(int j = 0; j<n;j++)
{
if(g[i][j] == 1)
{
e++;
}
}
}
result = e/n;
return result;
}
// 2. Is the graph a regular graph?
int isRegular(int **g, int n)
{
int e1 = 0, e2 = 0;
//checking 0th row
for (int i = 0; i < n; i++)
{
if (g[0][i] == 1)
{
e1++;
}
}
//checking 1st row and continuing if equal
for (int i = 1; i < n; i++)
{
e2 = 0;
for (int j = 0; j < n; j++)
{
if (g[i][j] == 1)
{
e2++;
}
}
if (e1 == e2)
continue;
else
return 0;
}
return 1;
}
// 3. Is the graph a complete graph?
int isComplete(int **g, int n)
{
int e = 0;
for(int i = 0; i<n ; i++)
{
e = 0;
for(int j = 0; j>n; j++)
{
if(g[i][j]==1)
e++;
}
}
if(e == (n*(n-1))/2)
return 1;
else
return 0;
}
// 4. Is the graph a cycle graph?
int isCycleGraph(int **g, int n)
{
int e = 0;
for (int i = 0; i < n; i++)
{
e = 0;
for (int j = 0; j < n; j++)
{
if (g[i][j] == 1)
{
e++;
}
}
if (e!= 2)
return 0;
}
return 1;
}
// 5. Is the graph a path graph but not a cycle graph?
int isPathGraph(int **g, int n)
{
int a = 0;
int b = 0;
for (int i = 0; i< n; i++)
{
a = 0;
for (int j = 0; j < n; j++)
{
if (g[i][j] == 1)
a++;
}
if (a > 2)
return 0;
else
{
if (a == 1)
b++;
}
}
if (b != 2)
return 0;
else
return 1;
return 0;
}
// 6. Does the graph has an Euler circuit?
int hasEulerCkt(int **g, int n)
{
int e = 0;
for (int i = 0; i< n; i++)
{
e = 0;
for (int j = 0; j < n; j++)
{
if (g[i][j] == 1)
{
e++;
}
}
if (e % 2 == 0)
continue;
else
return 0;
}
return 1;
}
// 7. Does the graph has an Euler path but not an Euler circuit?
int hasEulerPath(int **g, int n)
{
int a = 0;
int b = 0;
for (int i = 0; i < n; i++)
{
a = 0;
for (int j = 0; j < n; j++)
{
if (g[i][j] == 1)
{
a++;
}
}
if (a%2 == 0)
continue;
else
b++;
}
if (b != 2)
return 0;
else
return 1;
}
// 8. Does the graph satisfy the sufficient condition of the Ore's theorem?
// Sufficient condition for the graph to have a Hamilton according the Ore's theorem:
// deg(u) + deg(v) ≥ n for every pair of nonadjacent vertices u and v in the graph
//user-defined function to calculate degree
int degree(int **g, int n, int v)
{
int e = 0;
for (int i = 0; i < n; i++)
{
if (g[v][i] != 1)
continue;
else
e++;
}
return e;
}
int satifiesOresTheorem(int **g, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (g[i][j] != 0)
continue;
else
{
if ((degree(g, n, i) + degree(g, n, j)) >= n)
continue;
else
return 0;
}
}
}
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.