Quantcast
Channel: Active questions tagged dijkstra - Stack Overflow
Viewing all articles
Browse latest Browse all 57

shortest path algorithm with expected cost

$
0
0

My objective is to calculate the expected cost for each path from the starting point to the end point and identify the minimum expected cost for each path when the respective node serves as the starting point.

So fistly : I have to find all possible paths

secondly : calculate expected cost for each path

Action_type_1 can be the end point, while Action_type_2 cannot serve as the end point.

Action_type 1 includes actions A and B, whereas Action_type 2 comprises actions C and D.

Each node is assigned a cost, assumed to be 1 for all nodes.

We can conceptualize the paths to the end point as follows:

If A is the starting point:

A -> B

A -> C -> B

If B is the starting point:

B -> A

B -> D -> A

The same applies to C and D, such as

C -> A -> D -> B.

Here is an example:

enter image description here

When comparing the expected costs of two paths, I can select the path from A to B with the lower expected cost."

I have a python code to calculate expected cost for each path :

def expected_cost(graph, node, end_nodes):   if node in end_nodes:        return graph[node]['cost']   total_cost = 0   for neighbor, probability in graph[node]['neighbors'].items():        total_cost += probability * (graph[node]['cost'] + expected_cost(graph, neighbor, end_nodes))   return total_costgraph1 = {'A': {'cost': 1, 'neighbors': {'end': 0.5, 'B': 0.5}},'end': {'cost': 0, 'neighbors': {}},'B': {'cost': 1, 'neighbors': {'end': 1}},  # End Node}graph2 = {'A': {'cost': 1, 'neighbors': {'end': 0.5, 'C': 0.5}},'end': {'cost': 0, 'neighbors': {}},'B_yes': {'cost': 1, 'neighbors': {'end': 1}},  # End Node'B_no': {'cost': 1, 'neighbors': {'end': 1}},  # End Node'C': {'cost': 1, 'neighbors': {'B_yes': 0.8, 'B_no': 0.2}},}end_nodes = ['end']expected_cost_A_1 = expected_cost(graph1, 'A', end_nodes)print("Expected cost at node A of graph1:", expected_cost_A_1)expected_cost_A_2 = expected_cost(graph2, 'A', end_nodes)print("Expected cost at node A of graph2:", expected_cost_A_2)

Expected cost at node A of graph1: 1.5

Expected cost at node A of graph2: 2.0

But, it is not clear :

  1. To find all possible path. I think it is related with permutation. But its big O is O(nCr)...maybe we need some heuristic approch ?

  2. Calculate expected cost for each path. I think we can use some existing shortest path find algorithm like dijkstra algorithm.

Can you help me ?


Viewing all articles
Browse latest Browse all 57

Latest Images

Trending Articles





Latest Images