c++ - Depth First Search on Adjacency Matrix -


for program, given set of inputs need store in adjacency matrix. i've done this, have adjacency matrix matrix[11][11]. now, using matrix, need perform depth first search , return pi values.

i have pseudocode this, believe need 2 methods: dfs(graph) , dfs-visit(node). however, i'm having trouble implementing this. can using adjacency matrix directly or somehow need create graph using matrix? coding appreciated.

dfs(g)     each u ∈ v[g]        color[u] = white         ∏[u] = nil     time = 0     each u ∈ v[g]        if color[u] = white           dfs-visit(u)   dfs-visit(u)     color[u] = gray     time++     d[u] = time     each v ∈ adj[u]        if color[v] = white           ∏[v] = u           dfs-visit(v)     color[u] = black    time++     f[u] = time 

the pseudo-code have there seems assume adjacency list.

specifically code: (indentation corresponding code blocks assumed)

for each v ∈ adj[u]    if color[v] = white      ∏[v] = u      dfs-visit(v)  

the difference is: adjacency matrix, vertices there, , 1 typically uses 0/1 flags indicate whether there's edge between current , target vertices.

so, should loop through vertices row in adjacency matrix, , when flag set 1.

that part of pseudo-code like:

for v = 1 n  // assuming 1-indexed   if color[v] = white && adjmatrix[u][v] == 1      ∏[v] = u      dfs-visit(v)  

as far can tell, rest of psuedo-code should identical.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -