In a given graph G=(V,E)
each edge has a cost c(e)
. We have a starting node s and a target node t. How can we find the most expensive path from s to t using following DFS algorithm?
DFS(G,s):
foreach v in V do
color[v] <- white; parent[v] <- nil
DFS-Visit(s)
DFS-Visit(u)
color[u] <- grey
foreach v in Adj[u] do
if color[v] = white then
parent[v] = u; DFS-Visit(v)
color[u] <- black
What I have tried:
So first we create an array to maintain the cost to each node:
DFS(G,s,t):
foreach v in V do
color[v] <- white; parent[v] <- nil; cost[v] <- -inf
DFS-Visit(s,t)
return cost[t]
Second we should still visit a node event if it is gray to update its cost:
DFS-Visit(u,t)
color[u] <- grey
foreach v in Adj[u] do
if color[v] != black then
parent[v] = u;
if cost[u] < cost[v] + c(u,v) then
cost[v] = cost[v] + c(u,v)
if t != v then
DFS-Visit(v)
color[u] <- black
and we don't want to go pass t. What do you think? Is my approach correct?