Skip to main content
edited body
Source Link
user46831
  • 663
  • 3
  • 8

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Added in response to User293787'aUser293787's good suggestion:


ClearAll[myShortestPathTree];
(* Given a graph and a vertex in the graph, 
   Return a graph consisting of the edges of a shortest path tree from that vertex
   The optional value DirectEdges determines whether the tree is directed or not *) 
myShortestPathTree[graph_,root_,OptionsPattern[{DirectEdges->True}]]:=
With[(*FindShortestPath returns a procedure that, given a vertex, returns a shortest path to it *)
     {pathfunc=FindShortestPath[graph,root,All],
      directedges = OptionValue[DirectEdges]},
     (* FindShortestPath returns a list of vertices, not the edges of the path, 
        that leaves ambiguity as to whether the edges of the path are directed or not.
        In a multigraph, the list of vertices does not uniquely identify the edges 
        The option *DirectEdges, with default value of True, allows the user produce either a directed spanning tree (DirectEdges -> True) or an undirected one*)
    Graph[DeleteDuplicates[
             Flatten[
                   Map[EdgeList[PathGraph[pathfunc[#], DirectedEdges->directedges]]&,
                       VertexList[graph]]]], 
          VertexLabels->"Name"]]
```

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Added in response to User293787'a good suggestion:


ClearAll[myShortestPathTree];
(* Given a graph and a vertex in the graph, 
   Return a graph consisting of the edges of a shortest path tree from that vertex
   The optional value DirectEdges determines whether the tree is directed or not *) 
myShortestPathTree[graph_,root_,OptionsPattern[{DirectEdges->True}]]:=
With[(*FindShortestPath returns a procedure that, given a vertex, returns a shortest path to it *)
     {pathfunc=FindShortestPath[graph,root,All],
      directedges = OptionValue[DirectEdges]},
     (* FindShortestPath returns a list of vertices, not the edges of the path, 
        that leaves ambiguity as to whether the edges of the path are directed or not.
        In a multigraph, the list of vertices does not uniquely identify the edges 
        The option *)
    Graph[DeleteDuplicates[
             Flatten[
                   Map[EdgeList[PathGraph[pathfunc[#], DirectedEdges->directedges]]&,
                       VertexList[graph]]]], 
          VertexLabels->"Name"]]
```

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Added in response to User293787's good suggestion:


ClearAll[myShortestPathTree];
(* Given a graph and a vertex in the graph, 
   Return a graph consisting of the edges of a shortest path tree from that vertex
   The optional value DirectEdges determines whether the tree is directed or not *) 
myShortestPathTree[graph_,root_,OptionsPattern[{DirectEdges->True}]]:=
With[(*FindShortestPath returns a procedure that, given a vertex, returns a shortest path to it *)
     {pathfunc=FindShortestPath[graph,root,All],
      directedges = OptionValue[DirectEdges]},
     (* FindShortestPath returns a list of vertices, not the edges of the path, 
        that leaves ambiguity as to whether the edges of the path are directed or not.
        In a multigraph, the list of vertices does not uniquely identify the edges 
        The option DirectEdges, with default value of True, allows the user produce either a directed spanning tree (DirectEdges -> True) or an undirected one*)
    Graph[DeleteDuplicates[
             Flatten[
                   Map[EdgeList[PathGraph[pathfunc[#], DirectedEdges->directedges]]&,
                       VertexList[graph]]]], 
          VertexLabels->"Name"]]
```
Added simple myShortestPathTree code in response to user293787's request
Source Link
user46831
  • 663
  • 3
  • 8

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Added in response to User293787'a good suggestion:


ClearAll[myShortestPathTree];
(* Given a graph and a vertex in the graph, 
   Return a graph consisting of the edges of a shortest path tree from that vertex
   The optional value DirectEdges determines whether the tree is directed or not *) 
myShortestPathTree[graph_,root_,OptionsPattern[{DirectEdges->True}]]:=
With[(*FindShortestPath returns a procedure that, given a vertex, returns a shortest path to it *)
     {pathfunc=FindShortestPath[graph,root,All],
      directedges = OptionValue[DirectEdges]},
     (* FindShortestPath returns a list of vertices, not the edges of the path, 
        that leaves ambiguity as to whether the edges of the path are directed or not.
        In a multigraph, the list of vertices does not uniquely identify the edges 
        The option *)
    Graph[DeleteDuplicates[
             Flatten[
                   Map[EdgeList[PathGraph[pathfunc[#], DirectedEdges->directedges]]&,
                       VertexList[graph]]]], 
          VertexLabels->"Name"]]
```

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...

Added in response to User293787'a good suggestion:


ClearAll[myShortestPathTree];
(* Given a graph and a vertex in the graph, 
   Return a graph consisting of the edges of a shortest path tree from that vertex
   The optional value DirectEdges determines whether the tree is directed or not *) 
myShortestPathTree[graph_,root_,OptionsPattern[{DirectEdges->True}]]:=
With[(*FindShortestPath returns a procedure that, given a vertex, returns a shortest path to it *)
     {pathfunc=FindShortestPath[graph,root,All],
      directedges = OptionValue[DirectEdges]},
     (* FindShortestPath returns a list of vertices, not the edges of the path, 
        that leaves ambiguity as to whether the edges of the path are directed or not.
        In a multigraph, the list of vertices does not uniquely identify the edges 
        The option *)
    Graph[DeleteDuplicates[
             Flatten[
                   Map[EdgeList[PathGraph[pathfunc[#], DirectedEdges->directedges]]&,
                       VertexList[graph]]]], 
          VertexLabels->"Name"]]
```
Source Link
user46831
  • 663
  • 3
  • 8

Thanks for that. Impressive. An alternative that might be better reflected in the documentation is to use FindShortestPath.

(* FindShortestPath returns a procedure *)
With[{pathfunc=FindShortestPath[smallgraph, 1, All],
     (* Construct weight rules for the graph *)
      edgeweights =Thread[Rule[EdgeList[smallgraph],
                               PropertyValue[smallgraph,EdgeWeight]]]},
     (* For each vertex, recover the path and its length *)
     Map[With[{path=EdgeList[PathGraph[pathfunc[#],
                                     DirectedEdges->True]]},
                {# , path, Plus@@path/.edgeweights}]&,
         VertexList[smallgraph]]]

Alternatively, just return the edges of the tree...