Re: WeightingFunction and Showing Graphs with wieghts as Edge Lable
- To: mathgroup at smc.vnet.net
- Subject: [mg83131] Re: WeightingFunction and Showing Graphs with wieghts as Edge Lable
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 12 Nov 2007 05:15:39 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <fh6d5q$4kr$1@smc.vnet.net>
mumat wrote: <snip> > Question 3. How can I graph a weighted Graph show the edgeweights show > up on the graph? for instance for this graph? > > In[1]=t = Wheel[8];k = SetEdgeWeights[t, WeightingFunction -> > RandomInteger, WeightRange -> {0, 4}] > > Any help would be greatly appreciated. Arguably, the easiest way might be to use Mathematica 6 built-in function *GraphPlot*. It accepts a list of pair of the form (v_out -> v_in, "edge_label"}. So, using your graph k, we are interested in transforming its first element k[[1]], a list of lists of the form {{v_out, v_in}, EdgeWeight -> edge_weight} (see Out[4]), into the required format for GraphPlot. To do so, we just use a transformation rule that we apply to k[[1]] (see In[5]) before calling GraphPlot. In[1]:= Needs["Combinatorica`"] t = Wheel[8]; k = SetEdgeWeights[t, WeightingFunction -> (RandomChoice[{2, 3, 5, 7}] &)]; Edges[k, EdgeWeight]; In[4]:= k[[1]] Out[4]= {{{1, 8}, EdgeWeight -> 3}, {{2, 8}, EdgeWeight -> 5}, {{3, 8}, EdgeWeight -> 3}, {{4, 8}, EdgeWeight -> 5}, {{5, 8}, EdgeWeight -> 7}, {{6, 8}, EdgeWeight -> 3}, {{7, 8}, EdgeWeight -> 7}, {{1, 2}, EdgeWeight -> 3}, {{2, 3}, EdgeWeight -> 3}, {{3, 4}, EdgeWeight -> 7}, {{4, 5}, EdgeWeight -> 2}, {{5, 6}, EdgeWeight -> 7}, {{6, 7}, EdgeWeight -> 2}, {{1, 7}, EdgeWeight -> 2}} In[5]:= k[[1]] /. {{p_Integer, q_Integer}, EdgeWeight -> r_} :> {p -> q, ToString@r} Out[5]= {{1 -> 8, "3"}, {2 -> 8, "5"}, {3 -> 8, "3"}, {4 -> 8, "5"}, {5 -> 8, "7"}, {6 -> 8, "3"}, {7 -> 8, "7"}, {1 -> 2, "3"}, {2 -> 3, "3"}, {3 -> 4, "7"}, {4 -> 5, "2"}, {5 -> 6, "7"}, {6 -> 7, "2"}, {1 -> 7, "2"}} {{1 -> 8, "2"}, {2 -> 8, "2"}, {3 -> 8, "3"}, {4 -> 8, "5"}, {5 -> 8, "7"}, {6 -> 8, "3"}, {7 -> 8, "2"}, {1 -> 2, "3"}, {2 -> 3, "5"}, {3 -> 4, "5"}, {4 -> 5, "7"}, {5 -> 6, "7"}, {6 -> 7, "7"}, {1 -> 7, "3"}} In[6]:= GraphPlot[%, VertexLabeling -> True] (Deleted Out[6] that shows a nice plot of a graph with the weight of each edge and the vertex numbers.) Regards, -- Jean-Marc