简体   繁体   中英

how to store a graph in a data structure when there are multiple ways to traverse two nodes

I would like to store a directed graph consisting of nodes and edges to design an algorithm. In addition to the traditional setting, there are multiple ways to travel between nodes. Suppose I pick a pair of nodes namely A and B . I can go from A to B with a car or a truck. My goal is to find all possible paths given two nodes using breadth first search (BFS). I was wondering how I should store my dataset to code BFS efficiently.

In a traditional setting, I store my network in a defaultdict as I share below.

class Network:
    def __init__(self, nodeNumber):
        self.V = nodeNumber
        self.network= defaultdict(list)
    def edgeConstruct(self, i, j):
        self.network[i].append(j)

My question is what could be the best way to store my network if there are multiple ways to travel between every two nodes. My goal is to find all possible ways to go from one location to another using different way of travel methods.

A B "car"
A B "truck"
B C "car"
B C "bus"
B C "bicycle"
...

You could define an edge per travel method, and specify that travel method as a label of the edge.

class Network:
    def __init__(self, nodeNumber):
        self.V = nodeNumber
        self.network= defaultdict(list)
    def edgeConstruct(self, i, j, travelmethod):
        self.network[i].append((j, travelmethod))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM