Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Label Graph Edges with their weights

Warning! I posted the question when Mathematica v 8.0 was the coolest kid. The bug has been solved as of version 9.0.1

The help for EdgeLabels states:

enter image description here

However:

CompleteGraph[4,
 EdgeWeight -> Range@6,
 VertexShapeFunction -> "Name",
 EdgeLabels -> "EdgeWeight"]

Results in:

enter image description here

So, no Edge Labels ... I guess it is a bug.

I used a nasty construct like:

adj = {{\[Infinity], 1, 1, 1, 1}, {1, \[Infinity], 2, 2, 2}, 
       {1, 2, \[Infinity], 2, 2}, {1, 2, 2, \[Infinity], 2}, 
       {1, 2, 2, 2, \[Infinity]}};

WeightedAdjacencyGraph[adj,
    VertexShapeFunction -> "Name", 
    EdgeLabels -> 
     MapThread[Rule,{EdgeList@#,AbsoluteOptions[#, EdgeWeight]/.{_ -> x_}-> x}], 
    GraphHighlight -> FindEdgeCover[#]]  
                                        &@ WeightedAdjacencyGraph[adj]

enter image description here

Better ideas?

like image 249
Dr. belisarius Avatar asked Sep 11 '11 20:09

Dr. belisarius


People also ask

What are weights of edges in graphs?

In many applications, each edge of a graph has an associated numerical value, called a weight. Usually, the edge weights are non- negative integers. Weighted graphs may be either directed or undirected. The weight of an edge is often referred to as the "cost" of the edge.

What do weights in a graph represent?

A weighted graph is a graph such that each edge is labeled with a number, called the weight of that edge. For example, the vertices of the graph above may represent certain towns in India, and the edges may represent roads between the towns, with their separation distances marked.

What is weighted graph in graph theory?

A weighted graph is a graph in which each branch is given a numerical weight. A weighted graph is therefore a special type of labeled graph in which the labels are numbers (which are usually taken to be positive).


1 Answers

For a regular GraphPlot, you will need a slightly more complicated solution using EdgeRenderingFunction (documentation). Suppose you have an adjacency matrix where the elements are also the (directional) weights.

lilnums = {{0, 2., 1., 3., 0, 6.}, {0, 0, 1., 2., 0, 0}, {1., 8., 0, 2., 0, 
 2.}, {10., 13., 7., 0, 0, 10.}, {0, 0, 0, 0, 0, 0}, {4., 1., 1., 2.,
 2., 0}}

Here are some labels for the vertices, supposing you are drawing network diagrams for international inter-bank exposures (the original has a lot more countries!).

names = {"AT", "AU", "CA", "CH", "CL", "ES"}

The following does what you need. The tricks are the reference back to the adjacency matrix using the parts of #2 inside the part specification, to reference the correct elements of nums, and the Mean[#1] to locate the label at the midpoint of the edge. The slot #1 seems to hold the coordinates of the vertices.

GraphPlot[lilnums, DirectedEdges -> True, 
 VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .04], 
 Black, Text[names[[#2]], #1]} &), 
 EdgeRenderingFunction -> ({AbsoluteThickness[2], Red, 
 Arrowheads[0.02], Arrow[#1, 0.05], Black, 
 Text[Round@ Abs[(lilnums[[#2[[1]], #2[[2]]]] + 
   lilnums[[#2[[2]], #2[[1]]]])], Mean[#1], 
  Background -> Yellow]} &), VertexLabeling -> True, 
 ImageSize -> 600,  
  PlotLabel -> Style["Plot Label", Bold, 14, FontFamily -> "Arial"]]

enter image description here

like image 62
Verbeia Avatar answered Sep 29 '22 23:09

Verbeia