patterncMinor
Strongly connected components, component graph, semiconnected graph and new graph from SCCs
Viewed 0 times
graphnewsemiconnectedstronglycomponentsconnectedsccsandcomponentfrom
Problem
I have written a program in C to do the following:
Read a matrix (in file format, first line you have the size, then next are the entries). This is done by the "input" function.
-
Find the strongly connected components using the kosaraju algorithm (the one that uses the transpose). Done by the "SCCmatrix":
+) Step 1: DFS all of the possible vertices, push them onto a stack
+) Step 2: Pop from the stack, DFS but this time do it on the transpose graph
-
Find a similar graph that has the same SCCs as the original one, with the same component graph and with the lowest number of edges possible. Here is how I do it (done by "minSCC") :
+) Find the component graph (this is easy since we have found all of the SCCs)
+) In each of the SCC, to make the edges as low as possible, you just connect all the vertices in a cycle
+) Use the component graph to connect the vertices in each of the SCC to each other (of course make sure that only one vertice from a SCC can connect to a vertex of another SCC)
-
The final one is done by "semiconnected". Here, the program finds out if the input graph is semiconnected (for all pairs of vertices u,v we have a road from u to v or a road from v to u, not necessarily means that the two are directly connected). It does that by using the component graph, which has a topological structure. It uses the first SCC used in step 2 of the Kosaraju algorithm and DFS from there, if all components are reached then it is semiconnected.
My reasonings are correct, right? Also, I would like you to review on my C code, since I find it rather cumbersome, and it seems to me that I have been going the wrong way in structural programming lately (and give me some tips as well please!). I know these are very easy problems but I'm not very good at graphs and algorithms in general.
```
#include
#include
#define MAX 100
//int time = 0;
int input(int matrix, int N);
int SCCmatrix(const int matrix, int N, int result, int component,int num);
int dfs
Read a matrix (in file format, first line you have the size, then next are the entries). This is done by the "input" function.
-
Find the strongly connected components using the kosaraju algorithm (the one that uses the transpose). Done by the "SCCmatrix":
+) Step 1: DFS all of the possible vertices, push them onto a stack
+) Step 2: Pop from the stack, DFS but this time do it on the transpose graph
-
Find a similar graph that has the same SCCs as the original one, with the same component graph and with the lowest number of edges possible. Here is how I do it (done by "minSCC") :
+) Find the component graph (this is easy since we have found all of the SCCs)
+) In each of the SCC, to make the edges as low as possible, you just connect all the vertices in a cycle
+) Use the component graph to connect the vertices in each of the SCC to each other (of course make sure that only one vertice from a SCC can connect to a vertex of another SCC)
-
The final one is done by "semiconnected". Here, the program finds out if the input graph is semiconnected (for all pairs of vertices u,v we have a road from u to v or a road from v to u, not necessarily means that the two are directly connected). It does that by using the component graph, which has a topological structure. It uses the first SCC used in step 2 of the Kosaraju algorithm and DFS from there, if all components are reached then it is semiconnected.
My reasonings are correct, right? Also, I would like you to review on my C code, since I find it rather cumbersome, and it seems to me that I have been going the wrong way in structural programming lately (and give me some tips as well please!). I know these are very easy problems but I'm not very good at graphs and algorithms in general.
```
#include
#include
#define MAX 100
//int time = 0;
int input(int matrix, int N);
int SCCmatrix(const int matrix, int N, int result, int component,int num);
int dfs
Solution
Always use
On bigger codes, omitting these can cause really big problems that are not very easy to notice at first.
It's just better practice to put them there.
You don't really seem to have a definitive indentation size.
For some things, you indent a single space, which is way too little.
For other things, you indent 4 spaces, which is perfect.
I recommend that you stick with 4 spaces for everything.
Since your program is not accepting any command-line arguments, it is good practice to put
All that method declaring at the top of your code is very nice-ly formatted.
However, I believe that it is in the wrong space.
You should move all these declarations to their own header file.
This is highly debatable.
You should use the pre-increment operator, rather than the post-increment operator.
For example, this
over this
{s and }.On bigger codes, omitting these can cause really big problems that are not very easy to notice at first.
It's just better practice to put them there.
You don't really seem to have a definitive indentation size.
For some things, you indent a single space, which is way too little.
For other things, you indent 4 spaces, which is perfect.
I recommend that you stick with 4 spaces for everything.
Since your program is not accepting any command-line arguments, it is good practice to put
void in the parameter section of main.All that method declaring at the top of your code is very nice-ly formatted.
However, I believe that it is in the wrong space.
You should move all these declarations to their own header file.
This is highly debatable.
You should use the pre-increment operator, rather than the post-increment operator.
For example, this
++iover this
i++Context
StackExchange Code Review Q#85946, answer score: 2
Revisions (0)
No revisions yet.