Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more:
trying to convert the python code to c for betweenness centrality

What I have tried:

# Betweenness Centrality", by Ulrik Brandes.
from collections import deque

def brandes(V, A):
    "Compute betweenness centrality in an unweighted graph."
    # Brandes algorithm
    # see
    C = dict((v,0) for v in V)
    for s in V:
        S = []
        P = dict((w,[]) for w in V)
        g = dict((t, 0) for t in V); g[s] = 1
        d = dict((t,-1) for t in V); d[s] = 0
        Q = deque([])
        while Q:
            v = Q.popleft()
            for w in A[v]:
                if d[w] < 0:
                    d[w] = d[v] + 1
                if d[w] == d[v] + 1:
                    g[w] = g[w] + g[v]
        e = dict((v, 0) for v in V)
        while S:
            w = S.pop()
            for v in P[w]:
                e[v] = e[v] + (g[v]/g[w]) * (1 + e[w])
                if w != s:
                    C[w] = C[w] + e[w]
    return C
Updated 11-Jul-18 0:16am
Mohibur Rashid 10-Jul-18 19:21pm
Start from learning c
Rate this:
Please Sign up or sign in to vote.

Solution 1

We are not a code conversion service: and translating from one language to another does not normally produce good code in the target language anyway. A much better approach is to write new code in the target language using the original as a specification.

Either way: start by learning both C and Python pretty well: you will need it given that C does not have any concept of the dict data structure in Python...
Rate this:
Please Sign up or sign in to vote.

Solution 2

You must Learn C/C++ to transform the code. C++ is an extension of C. It isnt so hard, when you know another languages.

The obvious difference is that you use a lot of curly braces. ;-)

Your problem is that you use some functions which arent so common in C. My tip is that you search in the internet for some replacement code. Check whethe you need really C or can use C++ and some class libraries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100