I had made my research about what are directed and undirected graphs. My approach is to use DFS algorithm. I know when visitedM[iVertices] is true or the node is visited twice it means it is not a tree. I also determine whether a graph is a tree I use the following theorem.
Theorem: Any connected graph with n vertices and n-1 edges is a tree
My code is initialize by reading from a file the number of vertices and number of edges as below
6 7
1 2
1 5
1 6
2 5
As you can see each edge is listed on one line with source vertex and destination vertex. Vertices start at 1. Edges are undirected and will be listed on the file once starting with the smallest vertex id. For example, for edges 2-5 and 5-2 the file will only have 2-5.
My problem is that I have no idea if i should allocate the node memory, how do I insert the node into the graph, how do I tell back my from DFS code is not a tree. I also have the void dfs as an int function that returns the count of visited vertices. int dft(grapg *g, int iV, int VisitedM[]). the function is similar to my void DFS, but it returns 0
if visitedM[iV], visitedM[iV] = TRUE, and returns iCount.
I also presume my data structure is messy.
#define MAX_VERTICES 100
typedef struct EdgeNode
{
int y;
int w; //weight
int iVertex //susbcript in vertexM for TO Vertex
struct EdgeNode *pNextEdge;
}EdgeNode;
typedef struct Vertex
{
char szLabel[100 +1 ];
EdgeNode *successorList;
}Vertex;
typedef struct
{
int iNumVertices;
int iNumEdges;
int iDirected;
Vertex vertexM[MAX_VERTICES +1];
int iParent[MAX_VERTICES + 1];
int iVisitedM[MAX_VERTICES + 1];
int degree[MAX_VERTICES + 1];
EdgeNode *edge[MAX_VERTICES +1];
}GraphImp;
typedef GraphImp *Graph;
int main(int argc, char *argv[])
{
FILE *pFile;
Graph graph;
char szInputBuffer[100];
int numEdges, numVertices;
pFile = fopen("a.txt","r");
if( pFile == NULL)
{
printf("FILE DOES NOT EXIST \n");
return;
}
//reads file until EOF
while(fscanf(pFile, "%d%d", &graph.iVertices, &graph.iEdges) != EOF)
{
//printf is to track the input from file
printf("num of vertices: %d \n num of edeges: %d \n",graph.iVertices, graph.iEdges);
}
fclose(pFile);
}
void dft(Graph *g, int iV, int visitedM[])
{
EdgeNode *e;
if(visitedM[iV])
return;
for( e = g->vertexM[iV].sucessorList; e!= NULL; e = e->pNextEdge)
{
dft(g, e->iVertex, visitedM);
}
}