2019-09-30 15:29:37 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-28 18:25:37 +03:00
|
|
|
#define MAX_SIZE 40 // Assume 40 nodes at max in graph
|
2020-05-29 23:23:24 +03:00
|
|
|
#define INT_MIN 0
|
|
|
|
// A vertex of the graph
|
2019-09-30 15:29:37 +03:00
|
|
|
struct node
|
|
|
|
{
|
|
|
|
int vertex;
|
2020-05-29 23:23:24 +03:00
|
|
|
struct node *next;
|
2019-09-30 15:29:37 +03:00
|
|
|
};
|
2020-05-29 23:23:24 +03:00
|
|
|
// Some declarations
|
|
|
|
struct node *createNode(int v);
|
2019-09-30 15:29:37 +03:00
|
|
|
struct Graph
|
|
|
|
{
|
|
|
|
int numVertices;
|
2020-05-29 23:23:24 +03:00
|
|
|
int *visited;
|
|
|
|
struct node *
|
2020-06-28 18:25:37 +03:00
|
|
|
*adjLists; // we need int** to store a two dimensional array. Similary,
|
|
|
|
// we need struct node** to store an array of Linked lists
|
2019-09-30 15:29:37 +03:00
|
|
|
};
|
2020-05-29 23:23:24 +03:00
|
|
|
// Structure to create a stack, necessary for topological sorting
|
2019-09-30 15:29:37 +03:00
|
|
|
struct Stack
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int arr[MAX_SIZE];
|
|
|
|
int top;
|
2019-09-30 15:29:37 +03:00
|
|
|
};
|
2020-05-29 23:23:24 +03:00
|
|
|
struct Graph *createGraph(int);
|
|
|
|
void addEdge(struct Graph *, int, int);
|
|
|
|
void printGraph(struct Graph *);
|
|
|
|
struct Graph *transpose(struct Graph *);
|
|
|
|
void fillOrder(int, struct Graph *, struct Stack *);
|
|
|
|
void scc(struct Graph *);
|
|
|
|
void dfs(struct Graph *, int);
|
|
|
|
struct Stack *createStack();
|
|
|
|
void push(struct Stack *, int);
|
|
|
|
int pop(struct Stack *);
|
2019-09-30 15:29:37 +03:00
|
|
|
|
|
|
|
int main()
|
2020-05-29 23:23:24 +03:00
|
|
|
{
|
|
|
|
int vertices, edges, i, src, dst;
|
|
|
|
printf("Enter the number of vertices\n");
|
|
|
|
scanf("%d", &vertices);
|
|
|
|
struct Graph *graph = createGraph(vertices);
|
|
|
|
printf("Enter the number of edges\n");
|
|
|
|
scanf("%d", &edges);
|
|
|
|
for (i = 0; i < edges; i++)
|
|
|
|
{
|
|
|
|
printf("Edge %d \nEnter source: ", i + 1);
|
|
|
|
scanf("%d", &src);
|
|
|
|
printf("Enter destination: ");
|
|
|
|
scanf("%d", &dst);
|
|
|
|
addEdge(graph, src, dst);
|
|
|
|
}
|
|
|
|
printf("The strongly connected conponents are:\n");
|
|
|
|
scc(graph);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
// Uncomment below part to get a ready-made example
|
2019-09-30 15:29:37 +03:00
|
|
|
/*struct Graph* graph2 = createGraph(4);
|
|
|
|
addEdge(graph2, 0, 1);
|
|
|
|
addEdge(graph2, 1, 2);
|
|
|
|
addEdge(graph2, 2, 0);
|
|
|
|
addEdge(graph2, 2, 3);
|
|
|
|
printf("The strongly connected components are:\n");
|
|
|
|
scc(graph2);
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("\n");*/
|
2019-09-30 15:29:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Creates a topological sorting of the graph
|
|
|
|
void fillOrder(int vertex, struct Graph *graph, struct Stack *stack)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
graph->visited[vertex] = 1;
|
|
|
|
struct node *adjList = graph->adjLists[vertex];
|
|
|
|
struct node *temp = adjList;
|
|
|
|
// First add all dependents (that is, children) to stack
|
|
|
|
while (temp != NULL)
|
|
|
|
{
|
2019-09-30 15:29:37 +03:00
|
|
|
int connectedVertex = temp->vertex;
|
2020-05-29 23:23:24 +03:00
|
|
|
if (graph->visited[connectedVertex] == 0)
|
|
|
|
{
|
|
|
|
fillOrder(connectedVertex, graph, stack);
|
|
|
|
}
|
|
|
|
temp = temp->next;
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// and then add itself
|
|
|
|
push(stack, vertex);
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Transpose the adjacency list
|
|
|
|
struct Graph *transpose(struct Graph *g)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
struct Graph *graph =
|
2020-06-28 18:25:37 +03:00
|
|
|
createGraph(g->numVertices); // Number of vertices is same
|
2020-05-29 23:23:24 +03:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < g->numVertices; i++)
|
|
|
|
{
|
|
|
|
struct node *temp = g->adjLists[i];
|
|
|
|
while (temp != NULL)
|
|
|
|
{
|
2020-06-28 18:25:37 +03:00
|
|
|
addEdge(graph, temp->vertex, i); // Reverse all edges
|
2020-05-29 23:23:24 +03:00
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return graph;
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Recursive dfs aproach
|
|
|
|
void dfs(struct Graph *graph, int vertex)
|
|
|
|
{
|
|
|
|
struct node *adjList = graph->adjLists[vertex];
|
|
|
|
struct node *temp = adjList;
|
|
|
|
|
|
|
|
// Add vertex to visited list and print it
|
2019-09-30 15:29:37 +03:00
|
|
|
graph->visited[vertex] = 1;
|
|
|
|
printf("%d ", vertex);
|
2020-05-29 23:23:24 +03:00
|
|
|
|
|
|
|
// Recursively call the dfs function on all unvisited neighbours
|
|
|
|
while (temp != NULL)
|
|
|
|
{
|
2019-09-30 15:29:37 +03:00
|
|
|
int connectedVertex = temp->vertex;
|
2020-05-29 23:23:24 +03:00
|
|
|
if (graph->visited[connectedVertex] == 0)
|
|
|
|
{
|
|
|
|
dfs(graph, connectedVertex);
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
|
|
|
temp = temp->next;
|
2020-05-29 23:23:24 +03:00
|
|
|
}
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// Strongly connected components
|
|
|
|
void scc(struct Graph *graph)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
// Step I: Create a topological sort of the graph and store it in a stack
|
|
|
|
struct Stack *stack = createStack();
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < graph->numVertices; i++)
|
|
|
|
{
|
|
|
|
// Execute topological sort on all elements
|
|
|
|
if (graph->visited[i] == 0)
|
|
|
|
{
|
|
|
|
fillOrder(i, graph, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step 2: Get the transpose graph
|
|
|
|
struct Graph *graphT = transpose(graph);
|
|
|
|
// Step 3: Perform a simple dfs by popping nodes from stack
|
|
|
|
while (stack->top != -1)
|
|
|
|
{
|
|
|
|
int v = pop(stack);
|
|
|
|
if (graphT->visited[v] == 0)
|
|
|
|
{
|
|
|
|
dfs(graphT, v);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
|
|
|
|
// Allocate memory for a node
|
|
|
|
struct node *createNode(int v)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
struct node *newNode = malloc(sizeof(struct node));
|
2019-09-30 15:29:37 +03:00
|
|
|
newNode->vertex = v;
|
|
|
|
newNode->next = NULL;
|
|
|
|
return newNode;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Allocate memory for the entire graph structure
|
|
|
|
struct Graph *createGraph(int vertices)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
struct Graph *graph = malloc(sizeof(struct Graph));
|
2019-09-30 15:29:37 +03:00
|
|
|
graph->numVertices = vertices;
|
2020-05-29 23:23:24 +03:00
|
|
|
graph->adjLists = malloc(vertices * sizeof(struct node *));
|
2019-09-30 15:29:37 +03:00
|
|
|
graph->visited = malloc(vertices * sizeof(int));
|
2020-05-29 23:23:24 +03:00
|
|
|
|
2019-09-30 15:29:37 +03:00
|
|
|
int i;
|
2020-05-29 23:23:24 +03:00
|
|
|
for (i = 0; i < vertices; i++)
|
|
|
|
{
|
2019-09-30 15:29:37 +03:00
|
|
|
graph->adjLists[i] = NULL;
|
|
|
|
graph->visited[i] = 0;
|
|
|
|
}
|
|
|
|
return graph;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Creates a unidirectional graph
|
|
|
|
void addEdge(struct Graph *graph, int src, int dest)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
|
|
|
// Add edge from src to dest
|
2020-05-29 23:23:24 +03:00
|
|
|
struct node *newNode = createNode(dest);
|
2019-09-30 15:29:37 +03:00
|
|
|
newNode->next = graph->adjLists[src];
|
|
|
|
graph->adjLists[src] = newNode;
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Utility function to see state of graph at a given time
|
|
|
|
void printGraph(struct Graph *graph)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
for (v = 0; v < graph->numVertices; v++)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
struct node *temp = graph->adjLists[v];
|
2019-09-30 15:29:37 +03:00
|
|
|
printf("\n Adjacency list of vertex %d\n ", v);
|
|
|
|
while (temp)
|
|
|
|
{
|
|
|
|
printf("%d -> ", temp->vertex);
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Creates a stack
|
|
|
|
struct Stack *createStack()
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
struct Stack *stack = malloc(sizeof(struct Stack));
|
|
|
|
stack->top = -1;
|
2020-01-17 12:10:31 +03:00
|
|
|
return stack;
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Pushes element into stack
|
|
|
|
void push(struct Stack *stack, int element)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
stack->arr[++stack->top] =
|
2020-06-28 18:25:37 +03:00
|
|
|
element; // Increment then add, as we start from -1
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Removes element from stack, or returns INT_MIN if stack empty
|
|
|
|
int pop(struct Stack *stack)
|
2019-09-30 15:29:37 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
if (stack->top == -1)
|
|
|
|
return INT_MIN;
|
|
|
|
else
|
|
|
|
return stack->arr[stack->top--];
|
2019-09-30 15:29:37 +03:00
|
|
|
}
|