0

I am creating a graph with 5 nodes in it (A,B,C,D,E) and edges/weights ("A","D",1),("D","B",3),("E","D",5),("C","B",4),("B","E",2)

I want to create a function that will create edges AB,AC,AE,BC,CE for me by summing the edges along the path from (for example) AB, which would be BD with weight 3 + AD with weight 1.

What I mean is if AD is an edge with weight 1 and DB is an edge with weight 3, I want the function to create edge AB with weight 4. I have a graph and vertex class but do not know where to go from here.

class Vertex:
    def __init__(self, node):
        self.id = node
        self.adjacent = {}

    def __str__(self):
        return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])

    def add_neighbor(self, neighbor, weight=0):
        self.adjacent[neighbor] = weight

    def get_connections(self):
        return self.adjacent.keys()


    def get_id(self):
        return self.id

    def get_weight(self, neighbor):
        return self.adjacent[neighbor]

class Graph:
    def __init__(self):
        self.vert_dict = {}
        self.num_vertices = 0

    def __iter__(self):
        return iter(self.vert_dict.values())

    def add_vertex(self, node):
        self.num_vertices = self.num_vertices + 1
        new_vertex = Vertex(node)
        self.vert_dict[node] = new_vertex
        return new_vertex

    def get_vertex(self, n):
        if n in self.vert_dict:
            return self.vert_dict[n]
        else:
            return None

    def add_edge(self, frm, to, cost = 0):
        if frm not in self.vert_dict:
            self.add_vertex(frm)
        if to not in self.vert_dict:
            self.add_vertex(to)

        self.vert_dict[frm].add_neighbor(self.vert_dict[to], cost)
        self.vert_dict[to].add_neighbor(self.vert_dict[frm], cost)

    def get_vertices(self):
        return self.vert_dict.keys()

if __name__ == '__main__':

    g = Graph()

    g.add_vertex('a')
    g.add_vertex('b')
    g.add_vertex('c')
    g.add_vertex('d')
    g.add_vertex('e')

    allEdges = [("A", "D"), ("D", "B"), ("E", "D"), ("C", "B"), ("B", "E")]
    nodes = ["A", "B", "C", "D", "E"]

    g.add_edge('a', 'd', 1)
    g.add_edge('d', 'b', 3)
    g.add_edge('e', 'd', 5)
    g.add_edge('b', 'c', 4)
    g.add_edge('b', 'e', 2)

    for v in g:
        for w in v.get_connections():
            vid = v.get_id()
            wid = w.get_id()
            print '( %s , %s, %3d)'  % ( vid, wid, v.get_weight(w))

    for v in g:
        print 'g.vert_dict[%s]=%s' %(v.get_id(), g.vert_dict[v.get_id()])
busybear
  • 10,194
  • 1
  • 25
  • 42
  • 2
    Is there a reason you aren't using NetworkX? – busybear Mar 24 '20 at 23:00
  • A little off-topic, but why is you're name sledgehammer and you're profile picture a koala? It gives me a few suspicions... –  Mar 25 '20 at 00:39

1 Answers1

0

I don't know python, but here is the algorithm I thought of. Also, I assume you're talking about the shortest path between two vertices, because in an undirected Graph there are infinite ways to go from one Vertex to another. And I also assume you know how to calculate the shortest path from one node to all the other nodes, because that is a pretty complex topic, but if you don't some good searches would be Dikstra's shortest path algorithm or Floyd's shortest path algorithm. So, after you know all that, the algorithm is simple.

Start from a random source vertex, and then calculate the minimum weight from that vertex to all others. Then, one by one go to every vertex that the current vertex isn't connect to, and connect them with the minimum weight you calculated. Repeat this same process for all the other nodes.

Pseudo Code (which looks a lot like Java because thats the only language I know well, but I tried to make it understandable):

for(every Vertex v) {
    Map<Vertex, int> = min-path(v) // min path maps every Vertex with the min weight from v to that Vertex.

    for(every Vertex current that v isn't connected to) {
        connect v -> current with weight map.get-value(current)
    }
}