Group Abstract Group Abstract

Message Boards Message Boards

0
|
5K Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[Solved] Obtain the shortest paths in weighted graph efficiently?

Posted 5 years ago

POSTED BY: Richard Frost
6 Replies

Thanks to Daniel and Rohit for the tips ... which also led to the discovery of the WeightedAdjacencyGraph[] constructor. Here's what I discovered from some tests with my data ... all times local on a 5 yr-old Windows 10 box.

125 vertices, 7750 undirected weighted edges.
Unweighted graph, Graph[edges] ? 0.006 sec.
Weighted graph by list of weights Graph[vertices, edges, EdgeWeight -> edgeWeightsList] ? 0.006 sec.
Weighted graph by WeightedAdjacencyMatrix[vertices,weightedAdj] ? 0.05 sec.
Weighted graph by rules (e -> w) Graph[vertices, edges, EdgeWeight -> edgeWeightsRules] ? 1.8 sec.
Weighted graph by Annotation, Graph[annotatedEdges] ? 3.5 hours.

All the weighted graphs above have equivalent WeightedAdjacencyMatrix's, but the specialized constructor produces a graph containing self-edges that are infinitely weighted, so its graph object is not equal to the others.

Here's my test code, excluding the last one which ran overnight in a separate notebook.

POSTED BY: Richard Frost

What specifically do you do to find the paths? Are you computing paths between all vertex pairs?

POSTED BY: Daniel Lichtblau
POSTED BY: Richard Frost

I am still confused. Is the bottleneck the creation of the Graph objects? Or is it the application of FindShortestPath once they have been created?

--- edit ---

Sorry, I was not reading carefully. It is graph creation that is slow. I wonder if it might be more efficient to work directly with the sparse matrices that contain the edge weight information?

--- end edit ---

POSTED BY: Daniel Lichtblau
POSTED BY: Richard Frost
Posted 5 years ago

Hi Richard,

It seems like specifying the edge weights using Annotation causes the performance issue with creating the graph.

g = CompleteGraph[30];
edges = EdgeList@g;
weightedEdges = Annotation[#, EdgeWeight -> 1] & /@ edges;

(g1 = Graph[weightedEdges];) // Timing
(* 2.60342, Null} *)

(g2 = Graph[edges, EdgeWeight -> ConstantArray[1, Length@edges]];) // Timing
(* {0.000226, Null} *)

g1 == g2
(* True *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard