### 10973 - Triangle Counting

Posted:

**Fri Nov 11, 2005 11:56 pm**I couldn't find anything better than O(VE) in this problem. Can anybody explain a better algo please?

Regards

Sanny

Regards

Sanny

Page **1** of **3**

Posted: **Fri Nov 11, 2005 11:56 pm**

I couldn't find anything better than O(VE) in this problem. Can anybody explain a better algo please?

Regards

Sanny

Regards

Sanny

Posted: **Sat Nov 12, 2005 8:51 am**

me neither.

I just calculated by frute force with some optimizations. My algo depends on number of vertices. Since there isnt any edge into more than 2 triangles, you know there is no more edges than 3 times number of nodes. In general, it's better an algo O(3n*k) than O(n^2 * k).

I just calculated by frute force with some optimizations. My algo depends on number of vertices. Since there isnt any edge into more than 2 triangles, you know there is no more edges than 3 times number of nodes. In general, it's better an algo O(3n*k) than O(n^2 * k).

Posted: **Sat Nov 12, 2005 10:39 am**

Your assumption that the number of edges in such a graph is less than 3*V is wrong. Consider a complete bi-partite graph of V vertices with V/2 vertices on each part. This graph does not have any triangles and has V^2/4 edges.

Posted: **Sat Nov 12, 2005 4:13 pm**

Has anybody got AC in this problem using STL to keep the edge list or adjacency list?

Regards

Sanny

Regards

Sanny

Posted: **Sat Nov 12, 2005 5:18 pm**

I have to accept you are allright. Very nice sample. When i solved this problem, i was thinking what was the worst case for number of triangules . Well, at least my algo runs on time. I didn't expect such a case.Your assumption that the number of edges in such a graph is less than 3*V is wrong

Thank you, Arahimi.

Posted: **Sun Nov 13, 2005 10:28 am**

I got Runtime error...... why?

I used bool[3001][3001], is this too big?

I used bool[3001][3001], is this too big?

Posted: **Sun Nov 13, 2005 10:35 am**

No, my solution also uses such array.Wei-Ming Chen wrote:I used bool[3001][3001], is this too big?

Posted: **Sun Nov 13, 2005 10:51 am**

This is my code. Is where wrong?

I don't like RTE....

#include <stdio.h>

int main()

{

bool a[3001][3001];

int b,c,d,e,f,g,h;

I don't like RTE....

#include <stdio.h>

int main()

{

bool a[3001][3001];

int b,c,d,e,f,g,h;

Posted: **Sun Nov 13, 2005 11:02 am**

bool a[3001][3001] is too big for stack memory. Declare this as global and you'll be fine.

Regards

Sanny

Regards

Sanny

Posted: **Sun Nov 13, 2005 11:07 am**

You are right

I got Ac, thank you

I got Ac, thank you

Posted: **Thu Nov 24, 2005 3:58 pm**

Code: Select all

```
#include <iostream>
#include <cstdio>
using namespace std;
bool adj[3001][3001] = {false};
// Graph will be transformed into adjacently matrix.
void reset(); // Reset after each test case.
int main()
{
int t, n, m, a, b, i, j, k;
cin >> t;
while(t--) {
int triangle = 0; // The number of triangle
scanf("%d %d", &n, &m);
for(i = 1; i <= m; i++) {
scanf("%d %d", &a, &b);
adj[a][b] = true;
adj[b][a] = true;
}
// Count triangle in inputed graph.
for(i = 1; i <= m; i++) {
for(j = i + 1; j <= m; j++) {
for(k = j + 1; k <= m; k++) {
if(i != j && i != k && j != k) {
// Are I, J and K connected?
if(adj[i][j] && adj[j][k] && adj[i][k]) {
triangle++;
}
}
}
}
}
cout << triangle << endl;
}
return 0;
}
void reset()
{
int i, j;
for(i = 1; i <= 3000; i++) {
for(j = 1; j <= 3000; j++) {
adj[i][j] = false;
}
}
}
```

I think 'bool adj[3001][3001]' isn't such big that it raises stack overflow.

Hmm... I don't know why 'Runtime Error'. Please help me!

Posted: **Thu Nov 24, 2005 4:00 pm**

Your for-loops should go up to n, not to m.

Btw, you will get TLE with your method.

Btw, you will get TLE with your method.

Posted: **Sat Dec 31, 2005 1:41 pm**

Can someone give some test cases , thanks .Adrian Kuegel wrote:Your for-loops should go up to n, not to m.

Btw, you will get TLE with your method.

I can't find my error

Posted: **Sat Dec 31, 2005 2:00 pm**

What is your problem? WA or TLE?

the worst case for TLE is a bipartite graph. it has no triangle.

you can write a generator for this kind of input.

the worst case for TLE is a bipartite graph. it has no triangle.

you can write a generator for this kind of input.

Posted: **Sat Dec 31, 2005 2:14 pm**

sorry , I got wa , I think my method won't get tleMoha wrote:What is your problem? WA or TLE?

the worst case for TLE is a bipartit graph. it has no triangle.

you can write a generator for this kind of input.