0

i want to construct an undirected weighted graph (as a python dict), where weight is the Euclidean distance between two nodes connected by edges given in the Mapdata.csv file. I cant find anything on google. please help me. I want python code for creating weighted undirected graph.

To represent a graph in a file, we list the number of vertices and edges, then list the vertices (index followed by its x and y coordinates), then list the edges (pairs of vertices). For example, the following figure shows a graph and its file representation:

Imama Khan
  • 11
  • 1

1 Answers1

0

You can store the graph in whatever way is most useful and logical for your use case. I would not suggest a dict for this as it doesn't make much sense for a weight graph. Really I would use a 2D array so that adding an element means adding a new row and column, where you can calculate the new weights beforehand for later use...

       NODE 1 | NODE 2 | NODE 3
_______________________________________
NODE 1    0         2       4    

NODE 2    2         0       3

NODE 3    4         3       0

Note the diagonal symmetry.

It really depends on what the use is though. Do you need the (x,y) coordinates after the fact? What is the end goal?

J. Blackadar
  • 1,821
  • 1
  • 11
  • 18
  • The end goal is to search the shortest path. I have to do it with dict. please guide me. I have list of vertices and list of edges saperately. I want to construct the graph – Imama Khan Jul 16 '19 at 18:06
  • Okay, there are plenty of options for shortest path, but check out this example on Djikstra's algorithm: https://www.geeksforgeeks.org/python-program-for-dijkstras-shortest-path-algorithm-greedy-algo-7/. This will get you started. If you have to do it with a dict (I'm assuming a programming assignment?) Then you should be able to adapt the example to use one. Best of luck! – J. Blackadar Jul 16 '19 at 18:37