Lee Yaan
Lee Yaan

Reputation: 547

Edge classification using DFS

I create a graph using networkx and i want to add a 'class' attribute in every edge to know if is forward,back,cross or tree. As a next step i tried to implement the following pseudocode in python but when i print the graph edges, the output has empty 'class' attribute for every edge.

DFS (V, E)

1.     for each vertex u in V[G]
2.        do color[u] ← WHITE
3.                π[u] ← NIL
4.     time ← 0
5.     for each vertex u in V[G]
6.        do if color[u] ← WHITE
7.                then DFS-Visit(u)              ▷ build a new DFS-tree from u

DFS-Visit(u)         ▷ with edge classification. G must be a directed graph

1.        color[u] ← GRAY
2.        time ← time + 1
3.        d[u] ← time
4.        for each vertex v adjacent to u
5.            do if color[v] ← BLACK
6.                then if d[u] < d[v]
7.                            then Classify (u, v) as a forward edge
8.                            else Classify (u, v) as a cross edge
9.                        if color[v] ← GRAY
10.                            then Classify (u, v) as a back edge
11.                      if color[v] ← WHITE
12.                            then π[v] ← u
13.                                    Classify (u, v) as a tree edge
14.                                    DFS-Visit(v)
15.        color[u] ← BLACK
16.        time ← time + 1
17.        f[u] ← time

Here is my code:

def dfs_visit(u):
    color[u] = 'gray'
    global time
    time = time + 1
    d[u] = time
    for v in G.neighbors(u):
        if color[v] == 'black':
            if d[u] < d[v]:
                G.edges[u,v]['class'] = 'forward'
            else:
                G.edges[u,v]['class'] ='cross'
            if color[v] == 'gray':
                G.edges[u, v]['class'] = 'back'
            if color[v] == 'white':
                p[v] = u
                G.edges[u, v]['class'] = 'tree'
                dfs_visit(v)
    color[u] = 'black'
    time = time + 1
    f[u] = time



for u in G.nodes():
    color[u] = 'white'
time = 0
for u in G.nodes():
    if color[u] == 'white':
        dfs_visit(u)

print(G.edges(data=True))

If i print all the graph edges the i take the following output:

[(1, 2, {'class': ''}), (1, 6, {'class': ''}), (2, 3, {'class': ''}), (6, 3, {'class': ''}), (3, 4, {'class': ''}), (3, 1, {'class': 'cross'}), (3, 7, {'class': ''}), (4, 5, {'class': ''}), (4, 7, {'class': ''})]

The class attribute of every edge is empty. How can i fix it?

Upvotes: 0

Views: 1140

Answers (1)

rodgdor
rodgdor

Reputation: 2630

Might be an indentation problem, try the following:

def dfs_visit(u):
    color[u] = 'gray'
    global time
    time = time + 1
    d[u] = time
    for v in G.neighbors(u):
        if color[v] == 'black':
            if d[u] < d[v]:
                G.edges[u,v]['class'] = 'forward'
            else:
                G.edges[u,v]['class'] ='cross'
        if color[v] == 'gray':
            G.edges[u, v]['class'] = 'back'
        if color[v] == 'white':
            p[v] = u
            G.edges[u, v]['class'] = 'tree'
            dfs_visit(v)
    color[u] = 'black'
    time = time + 1
    f[u] = time

Upvotes: 2

Related Questions