I have a sqlite table representing a weighted directed graph. The columns are as follows:
node1 | node2 | weight(node1,node2)
Instead I need this table:
node1 | node2 | weight(node1,node2) | weight(node2,node1)
(node2 | node1 | weight(node2,node1) | weight (node1, node2) should not exist in the table)
I am not familiar with SQL that well, so I couldn’t figure out how to make the new table from the original table in sqlite3.
Thanks for your help.
I assume your node1 and node2 are integer foreign keys linking to a node table, and the table you mention is an edge table?
Assuming the edge table has been created with something like:
How about something like (assuming no self-arcs and for every link from a->b there is also a link from b->a):
The self-join collates forward and backwards edges, and the requirement that e1.node1 is less than e1.node2 ensures that you only see each collated edge once.
Edit in response to a request to fill in zeros for missing backwards edge:
For missing backwards edges, you can do a LEFT JOIN and use a CASE statement to fill in the gaps with zeros:
Hope that helps!