The URL
http://www.infoarena.ro/multe-smenuri-de-programare-in-cc-si-nu-numai seems to contain useful algorithms in C/C++. (By "C/C++", I mean that the author uses C constructs but they're supposed to compile in C++ too. As in the subject title, some of it doesn't compile in either C or C++, though, using gnu).
The language of the original text is Romanian/C/C++ (I think) but Google Translate works superbly (which isn't to imply that other online translators aren't equally good or better)>
The relevant excerpt begins after "BEGIN QUOTE".
int N, M, G * [N], Deg [N]; doesn't compile, and I wouldn't expect it to compile either.
Can anyone suggest a correction? Also, I am trying to learn from this URL more generally, so I would be very interested if someone disagrees with anything on the URL or in the quote below, even if it isn't pertinent to my non-compilation problem.
Many thanks for your help,
Paul
BEGIN QUOTE
Graphs lists adjacency
It knows (or should know!) That working with pointers is very slow ... so they retain a graph rarely (number of nodes, small number of edges) with pointers (see below) greatly slow the program .
struct list
{
int n;
struct list * next;
}
typedef struct Lists;
In contiuare we present a method that is 3-4 times faster (ie Browsing DF, BF or other algorithms runs 3-4 times faster when the graph is stored so), but has the disadvantage need to read twice input file.
#include <stdlib.h>
#include <stdio.h>
// BELOW SEEMS WRONG AND IS AN ERROR ON MY COMPILER
// ****************************************************
int N, M, G * [N], Deg [N];
// ************************************************
int main ( void )
{
int i, j;
freopen ( "in.txt" , "r" , stdin);
scanf ( "% d% d" , & N & M);
for (; M> 0; M--)
{
scanf ( "% d% d" , & i, & j);
Deg [i - 1] ++, Deg [j - 1] ++;
}
for (i = 0; i <N; Deg [i ++] = 0)
G [i] = ( int *) malloc (Deg [i] * sizeof ( int ));
fseek (stdin, 0, SEEK_SET);
scanf ( "% d% d" , & N & M);
for (; M> 0; M--)
{
scanf ( "% d% d" , & i, & j);
i--, j--;
G [i] [Deg [i] ++] = j
G [j] [Deg [j] ++] = i;
}
return 0;
}
The speed increase is because the vectors are used instead of pointers and struct sites. If we can avoid reading memory allows twice the file by keeping the edges in a list of edges and then after calculating grades, inserting edges in lists. To demonstrate the effectiveness of this method do the following test: a source deploy and implement struct pointer and a BF, then write the code above with the following changes:
for (i = 0; i <N; i ++)
{
G [i] = ( int *) malloc ((Deg [i] +1) * sizeof ( int ));
G [i] [Deg [i]] = 1;
Deg [i] = 0;
}
and implement BF follows:
void BF ()
{
int Q [N], ql, qr, * p;
char U [N];
memset (U, 0, sizeof (U));
U [Q [ql = qr = 0] = n] = 1;
for (; ql <= qr; ql ++)
for (p = G [Q [ql]]; * p! = -1; p ++)
if (! U [* i]) U [Q [qr ++] = * p] = 1;
}
Then try to see the time difference between the two programs ... impressive, huh?
END QUOTE