Created
January 2, 2013 14:09
-
-
Save pdu/4434836 to your computer and use it in GitHub Desktop.
find the number of spanning trees in a cactus graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def dfs(buf, flag, nodes, start, cur): | |
if flag[cur]: | |
if cur == start and len(nodes) != 2 and min(nodes) == start: | |
return len(nodes) | |
else: | |
return -1 | |
flag[cur] = True | |
nodes.append(cur) | |
for adj in buf[cur]: | |
ret = dfs(buf, flag, nodes, start, adj) | |
if ret > 0: | |
return ret | |
nodes.pop() | |
flag[cur] = False | |
return -1 | |
def get_cactus(buf): | |
n = len(buf) | |
ret = 1 | |
for i in xrange(n): | |
flag = [False] * n | |
nodes = [] | |
tmp = dfs(buf, flag, nodes, i, i) | |
if tmp > 0: | |
ret *= tmp | |
return ret | |
def main(): | |
edges = [[1,2], [0,3], [0,3], [1,2,6,7], [6], [6], [3,4,5,7], [3,6]] | |
ret = get_cactus(edges) | |
print ret | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, can you explain it in more detail