MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: plotting weighted graphs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55382] Re: plotting weighted graphs
  • From: bghiggins at ucdavis.edu
  • Date: Mon, 21 Mar 2005 03:01:48 -0500 (EST)
  • References: <d1ecqp$euc$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Erik,
It is much easier to add edge weights using the combinatorica package.
I have a notebook that illustrates the procedure that I can send you if
that would help.

You can also do it with GraphPlot but it is more difficult. Here is how
I did it:

<< DiscreteMath`GraphPlot`;
myAdjMat = {{0, 1, 1,
    0, 1, 1}, {1, 0, 1, 1, 0, 1}, {1, 1, 0, 1,
       1, 0}, {0, 1, 1, 0, 1, 1}, {1, 0, 1, 1, 0, 1}, {1, 1, 0, 1, 1,
0}};

We will need some auxillary functions. First a function that defines
the weight for a given edge. I am asumming the adjacency matrix is
symmetric (non directed graph)

switchFunc[i_, j_] := Switch[{i, j}, {2, 1},
   "A", {3, 1}, "B", {5, 1}, "C", {6, 1}, "D", {3, 2}, "E", {4, 2},
"F", {
    6, 2}, "G", {4, 3}, "H", {5, 3}, "I", {5, 4}, "J", {6, 4}, "K
    ", {6, 5}, "L", _, ""]

I will also need the coordinates for the vertices:

coord = GraphCoordinates[myAdjMat]

Then we define an edgelabel function that returns a graphic primitive.
The arguments for this function are the edge variables,

edgelabel2[i_, j_] := Text[switchFunc[i, j], (coord[[i]] +
coord[[j]])/2]

Then we define the edge style function

esf[i_, j_] := Block[{}, {{Blue, Line[{i, j}]}, {Red, edgelabel2[i,
j]}}]

and finally we plot the graph

GraphPlot[myAdjMat, EdgeStyleFunction -> esf]

Hope this helps,

Cheers,
Brian




Erik Itter wrote:
> Hi, I have a hopefully quite easy question:
>     how do you plot graphs with weighted edges?
>
> I have the matrix specified by its adjadcency matrix using different
numbers as weights, works so
> far. Now I would like to plot the matrix numbering the vertices from
1 to n and label the edges with
> their weights. Its 5.1 therefore I think I could use GraphPlot (which
draws the graph with no
> labeling at all at the moment [but colors depending on weights]) if I
knew the proper
> EdgeStyleFunction and VertexStyleFunction.
> 
> 
> 
> All suggestions welcome.


  • Prev by Date: Re: Do loop is faulty
  • Next by Date: Re: Do loop is faulty
  • Previous by thread: Re: plotting weighted graphs
  • Next by thread: Multiple > and < calculations [great than & less than]