Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.6K Views
|
15 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Plot Graph-objects with TreePlot?

Posted 9 years ago
POSTED BY: Werner Geiger
15 Replies
Posted 9 years ago
POSTED BY: Werner Geiger

Oops! I misread it! it does support it, and exactly by the code I presented above. Ok, so that IS officially supported.

When I run into these things I always look at the examples, it generally gives a good feel about what the various inputs/outputs are.

Good to hear you got it resolved!

POSTED BY: Sander Huisman
POSTED BY: Sander Huisman

To add:

Note that in the TreePlot documentation a Graph object is not a valid input. There is however some 'hidden' (undocumented) functionality inside the definition of Treeplot. Namely:

TreePlot[g_ ? GraphQ, expr___] := Block[{res, adj, opts},
    adj = AdjacencyMatrix @ g;
    Condition[
       opts = opts = toTreePlotOptions @ g;
       res = TreePlot[adj, expr, opts];
       res /; UnsameQ[res, $Failed],
       MatrixQ[adj]
    ]
]

You can see how that it can handle Graph objects in some ways, but note that this is undocumented and might as well get removed in future versions! It might be some remnants from development (opts = opts = makes me think this).

POSTED BY: Sander Huisman
Posted 9 years ago
POSTED BY: Werner Geiger
Posted 9 years ago

Standard Graph- or TreeGraph-objects or their defining data are obviously useless. It's a horrible mess.

I want to solve some problem, but not debug WL.

POSTED BY: Werner Geiger
Posted 9 years ago

Sorry for plaguing you with my funny but probably nonsense problems. I have enough of digging into all those strange features or bugs within WL concerning graphs. I will just maintain my own abstract data structure defining a graph and then convert this to whatever the WL-functions like to have.

POSTED BY: Werner Geiger
Posted 9 years ago

I am really stuck. Bugs or not within all those WL-functions defining and exploring or plotting graphs, I cannot understand them. Maybe my test-program got too complicated. I reduced it to almost nothing.

What is wrong with the following trivial code? (Sorry I could not find how to show code and its output in a reasonable way in this forum).

{graphDataVertex, graphDataEdge} = {
  {Labeled[a, "S=a"], b1, b2, Labeled[c, "E=c"], Labeled[c2, "E=c2"]},
  {Labeled[a \[DirectedEdge] b1, "a,b1"], 
   Labeled[a \[DirectedEdge] b2, "a,b2"], b1 \[DirectedEdge] c, 
   b2 \[DirectedEdge] c2}
  }
graph = Graph[graphDataVertex, graphDataEdge, ImageSize -> Tiny]
FindPath[graph, a, c, Infinity, All]
TreePlot[graph, Left, a]
treeGraph = 
 TreeGraph[graphDataVertex, graphDataEdge, ImageSize -> Tiny]
FindPath[treeGraph, a, c2, Infinity, All]
TreePlot[treeGraph, Left, a]

Strange enough the following TreePlot-calls without startnode and its position work. But why?

TreePlot[graph]
TreePlot[treeGraph]
POSTED BY: Werner Geiger
Posted 9 years ago
POSTED BY: Werner Geiger
Posted 9 years ago

@ Werner: "->" is not the same as \DirectedEdge, otherwise I would have no reason to changed it. Did you run the code with my corrections in it (as posted above)?

POSTED BY: Michael Helmle

Word "bug" should not be placed in the title unless it is proven to be a bug. Reading the "details" section for the functions will help your understanding.

Docs clearly say (details section):

TreePlot returns a Graphics object.

so how can you expect it to be a Graph object?

Also, if you need a Graph object, what is wrong with TreeGraph or general structured embeddings for layered graphs such as trees and directed acyclic graphs:

Graph[..., GraphLayout -> "LayeredEmbedding"]
Graph[..., GraphLayout -> "LayeredDigraphEmbedding"]
Graph[..., GraphLayout -> "RadialEmbedding"]
Graph[..., GraphLayout -> "BalloonEmbedding"]

Also docs clearly say (details section):

TreePlot generates a tree plot of the graph g.

And here it is:

TreePlot[TreeGraph[{1 -> 2, 1 -> 3, 1 -> 4}]]
TreePlot[RandomGraph[{15, 34}]]
POSTED BY: Sam Carrettie
Posted 9 years ago
POSTED BY: Werner Geiger
Posted 9 years ago

I'm afraid the solution is to record the graphData in some totally abstract form like {vertices,edges}={{{vertex,label},...},{{edge,label},...}} and then convert this for TreePlot and Graph/FindPath as desired.

Pretty stupid.

POSTED BY: Werner Geiger
Posted 9 years ago

Hello Werner,

replace

graphData = {Labeled[a -> b1, "a,b1"], Labeled[a -> b2, "a,b2"], 
   b1 -> c, b2 -> c};

by

graphData = {Labeled[a \[DirectedEdge] b1, "a,b1"], 
   Labeled[a \[DirectedEdge] b2, "a,b2"], b1 \[DirectedEdge] c, 
   b2 \[DirectedEdge] c};

Treeplot requires a graph not just graphdata:

Print[TreePlot[Graph[graphData], 
   PlotLabel -> Style["1.1 TreePlot from graphData", Blue, Bold], 
   DirectedEdges -> True, VertexLabeling -> True, 
   EdgeLabeling -> True]];

Note that I also took out the "a". With these changes the 1st part should run.

I copied the changed version in below:

If[True,(*Test Graph (from Graph-Data)& TreePlot& FindPath*)
 Block[{a, b1, b2, c, graphData, 
   graph},(*Define graphData as a labeled list of edges in a form \
suitable for Graph*)
  Print[Style["1 Defining a graph by graphData suitable for Graph", 
    Red, Bold]];
  (* graphData={Labeled[a\[Rule]b1,"a,b1"],Labeled[a\[Rule]b2,"a,b2"],
  b1\[Rule]c,b2\[Rule]c};*)
  graphData = {Labeled[a \[DirectedEdge] b1, "a,b1"], 
    Labeled[a \[DirectedEdge] b2, "a,b2"], b1 \[DirectedEdge] c, 
    b2 \[DirectedEdge] c};
  Echo[graphData, "1 graphData: "];
  (*Make a Graph-object from graphData*)graph = Graph[graphData];
  Echo[Row[{Head[graph], " ", graph}], 
   "1 make graph from graphData: "];
  (*TreePlot and FindPath from graphData*)(* Print[TreePlot[graphData,
  Left,a,PlotLabel\[Rule]Style["1.1 TreePlot from graphData",Blue,
  Bold],DirectedEdges\[Rule]True,VertexLabeling\[Rule]True,
  EdgeLabeling\[Rule]True]];*)
  Print[TreePlot[Graph[graphData], 
    PlotLabel -> Style["1.1 TreePlot from graphData", Blue, Bold], 
    DirectedEdges -> True, VertexLabeling -> True, 
    EdgeLabeling -> True]];
  (* Print[Style[
  Row[{"1.1 FindPath from graphData: All paths from a to c: ",
  FindPath[graphData,a,c,Infinity,All]}],Blue,Bold]];*)
  Print[Style[
    Row[{"1.1 FindPath from graphData: All paths from a to c: ", 
      FindPath[Graph[graphData], a, c, Infinity, All]}], Blue, Bold]];
  (*TreePlot and FindPath from graph*)
  Print[TreePlot[graph, Left, 
    PlotLabel -> Style["1.2 TreePlot from graph", Blue, Bold], 
    DirectedEdges -> True, VertexLabeling -> True, 
    EdgeLabeling -> True]];
  Print[Style[
    Row[{"1.2 FindPath from graph: All paths from a to c: ", 
      FindPath[graph, a, c, Infinity, All]}], Blue, Bold]];
  (*Define graphData as a labeled list of edges in a Form suitable \
for TreePlot*)
  Print[Style["2 Defining a graph by graphData suitable for TreePlot",
     Red, Bold]];
  (* graphData={{a\[Rule]b1,"a,b1"},{a\[Rule]b2,"a,b2"},b1\[Rule]c,
  b2\[Rule]c};*)
  graphData = {Labeled[a \[DirectedEdge] b1, "a,b1"], 
    Labeled[a \[DirectedEdge] b2, "a,b2"], b1 \[DirectedEdge] c, 
    b2 \[DirectedEdge] c};
  Echo[graphData, "2 graphData: "];
  (*Make a Graph-object from graphData*)graph = Graph[graphData];
  Echo[Row[{Head[graph], " ", graph}], 
   "2 make graph from graphData: "];
  (*TreePlot and FindPath from graphData*)(* Print[TreePlot[graphData,
  Left,a,PlotLabel\[Rule]Style["2.1 TreePlot from graphData",Blue,
  Bold],DirectedEdges\[Rule]True,VertexLabeling\[Rule]True,
  EdgeLabeling\[Rule]True]];*)
  Print[TreePlot[Graph[graphData], 
    PlotLabel -> Style["2.1 TreePlot from graphData", Blue, Bold], 
    DirectedEdges -> True, VertexLabeling -> True, 
    EdgeLabeling -> True]];
  Print[Style[
    Row[{"2.1 FindPath from graphData: All paths from a to c: ", 
      FindPath[Graph[graphData], a, c, Infinity, All]}], Blue, Bold]];
  (*TreePlot and FindPath from graph*)
  Print[TreePlot[graph, Left, 
    PlotLabel -> Style["2.2 TreePlot from graph", Blue, Bold], 
    DirectedEdges -> True, VertexLabeling -> True, 
    EdgeLabeling -> True]];
  Print[Style[
    Row[{"2.2 FindPath from graph: All paths from a to c: ", 
      FindPath[graph, a, c, Infinity, All]}], Blue, Bold]];]]
POSTED BY: Michael Helmle
Posted 9 years ago

"replace""

graphData = {Labeled[a -> b1, "a,b1"], Labeled[a -> b2, "a,b2"], 
   b1 -> c, b2 -> c};

"by"

graphData = {Labeled[a \[DirectedEdge] b1, "a,b1"], 
   Labeled[a \[DirectedEdge] b2, "a,b2"], b1 \[DirectedEdge] c, 
   b2 \[DirectedEdge] c};

Isn't that exactly the same?

"Treeplot requires a graph not just graphData:"

I think, that unfortunately TreePlot does not accept a general Graph-object and only some kind of very special graphData..

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard