I'm trying to implement a program to find the articulation point of an undirected graph using DFS. I'm missing out a basic pointer de-referencing concept to retrieve the adjacency vertex of a given vertex. Kindly rectify the erroneous statement.
#include <stdio.h>
#include <stdlib.h>
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define NIL -1
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
void addEdge(struct Graph* graph, int src, int dest)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
int isArticulationPoint(int,int,struct Graph*);
void APUtil(int, int*, int*, int*, int*, int*,struct Graph*);
The main program, including a sample graph.
int main()
{
int V = 9,myNum;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 4);
addEdge(graph, 0, 2);
addEdge(graph, 0, 3);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 7);
addEdge(graph, 1, 8);
addEdge(graph, 3, 4);
addEdge(graph, 3, 5);
addEdge(graph, 5, 6);
scanf("%d",&myNum);
printf("%s",isArticulationPoint(myNum,V,graph)? "Yes" : "No" );
return 0;
}
void APUtil(int u, int visited[], int disc[],
int low[], int parent[], int ap[],struct Graph* graph)
{
// A static variable is used for simplicity, we can avoid use of static
// variable by passing a pointer.
static int time = 0;
// Count of children in DFS Tree
int children = 0;
// Mark the current node as visited
visited[u] = 1;
// Initialize discovery time and low value
disc[u] = low[u] = ++time;
// Go through all vertices aadjacent to this
struct AdjListNode *i;
for (i = graph->array[u].head; i != NULL; ++i)
{
int v = i->dest; // v is current adjacent of u.Here i'm messed up
// If v is not visited yet, then make it a child of u
// in DFS tree and recur for it
if (!visited[v])
{
children++;
parent[v] = u;
APUtil(v, visited, disc, low, parent, ap,graph);
// Check if the subtree rooted with v has a connection to
// one of the ancestors of u
low[u] = MIN(low[u], low[v]);
// u is an articulation point in following cases
// (1) u is root of DFS tree and has two or more chilren.
if (parent[u] == NIL && children > 1)
ap[u] = 1;
// (2) If u is not root and low value of one of its child is more
// than discovery value of u.
if (parent[u] != NIL && low[v] >= disc[u])
ap[u] = 1;
}
// Update low value of u for parent function calls.
else if (v != parent[u])
low[u] =MIN(low[u], disc[v]);
}
}
int isArticulationPoint(int myNum,int V,struct Graph* graph)
{
// Mark all the vertices as not visited
int *visited = (int *)malloc(V*sizeof(int));
int *disc = (int *)malloc(V*sizeof(int));
int *low = (int *)malloc(V*sizeof(int));
int *parent = (int *)malloc(V*sizeof(int));
int *ap = (int *)malloc(V*sizeof(int)); // To store articulation points
// Initialize parent and visited, and ap(articulation point) arrays
int i;
for ( i = 0; i < V; i++)
{
parent[i] = NIL;
visited[i] = 0;
ap[i] = 0;
}
// Call the recursive helper function to find articulation points
// in DFS tree rooted with vertex 'i'
for ( i = 0; i < V; i++)
if (visited[i] == 0)
APUtil(i, visited, disc, low, parent, ap,graph);
// Now ap[] contains articulation points, return 1 or 0
if (ap[myNum] == 1)
return 1;
else return 0;
}
It's not easy to make you understand, but look here at my code. I hope you will find what you need.