2
$\begingroup$

From https://mathematica.stackexchange.com/a/109436/70384 , I modified the code to display the bipartite graph I want and how do I label my edge with the edge weight by manually specifying it? I use EdgeLabels -> Automatic which is not what I want. r


ClearAll["Global`*"]
colsNames = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
attrsNames = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

len1 = 13;
len2 = 13;
keys1 = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", 
   "M"};
keys2 = {"T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9", "T10", 
   "T11", "T12", "T13"};
step = 15;
lis1 = Table[{y1, 0}, {y1, step, step*len1, step}];
lis2 = Table[{y1, 30}, {y1, step, step*len2, step}];

vlabels = 
  Join[MapThread[#1 -> 
      Placed[{Style[#, 16, Bold], Style[#2, 14, Bold]}, {Center, 
        Below}] &, {keys1, colsNames}], 
   MapThread[#1 -> 
      Placed[{Style[#, 16, Bold], Style[#2, 14, Bold]}, {Center, 
        Above}] &, {keys2, attrsNames}]];

Options[bpGraph] = {VertexLabelStyle -> Large, VertexStyle -> Red, 
   EdgeStyle -> Thick, VertexSize -> {"Scaled", .06}, 
   ImageSize -> Medium, VertexCoordinates -> Automatic, 
   VertexLabels -> Placed["Name", Center], EdgeLabels -> Automatic};

bpGraph[nds_, edgs_, opts : OptionsPattern[{bpGraph, Graph}]] := 
 Graph[nds, edgs, 
  Sequence @@ 
   FilterRules[Join[{opts}, Options[bpGraph]], Options[Graph]]]

SetOptions[bpGraph, VertexCoordinates -> Join[lis1, lis2], 
  VertexLabels -> vlabels];

after = bpGraph[
  Join[keys1, keys2], {"A" -> "T12" , "B" -> "T3", "C" -> "T5", 
   "D" -> "T9", "E" -> "T5", "F" -> "T6", "G" -> "T7", "H" -> "T8", 
   "I" -> "T9", "J" -> "T10", "K" -> "T4", "L" -> "T12", 
   "M" -> "T13"}]

This is the modified source code. I would like to specify my edge in this way, for example, EdgeLabels -> {1 <-> 2 -> 45} where 45 is my weight. I don't know how to type the symbol connecting 1 and 2, usually I just copy and paste the middle symbol.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

You can typeset a directed edge between nodes x and y as x -> y or x \[DirectedEdge] y or DirectedEdge[x, y], and an undirected edge as x <-> y or x \[UndirectedEdge] y or UndirectedEdge[x, y].

When used with rules we need to parenthesize directed edges as in EdgeLabels -> {(x -> y) ->5} to force correct parsing:

bpGraph[Join[keys1, keys2], {"A" -> "T12", "B" -> "T3", "C" -> "T5", 
   "D" -> "T9", "E" -> "T5", "F" -> "T6", "G" -> "T7", "H" -> "T8", 
   "I" -> "T9", "J" -> "T10", "K" -> "T4", "L" -> "T12", "M" -> "T13"},
  ImageSize -> Large,
  EdgeLabels -> {("B" -> "T3") -> 2, "C" \[DirectedEdge] "T5" -> 3, 
     DirectedEdge["E", "T5"] -> 4}]

enter image description here

For undirected edges we don't need parantheses

bpGraph[Join[keys1, keys2], {"A" -> "T12", "B" <-> "T3", "C" <-> "T5",
   "D" -> "T9", "E" <-> "T5", "F" <-> "T6", "G" -> "T7", "H" -> "T8", 
  "I" -> "T9", "J" -> "T10", "K" -> "T4", "L" -> "T12", "M" -> "T13"},
 ImageSize -> Large,
 EdgeLabels -> {"B" <-> "T3" -> 2, "C" \[UndirectedEdge] "T5" -> 3, 
  UndirectedEdge["E", "T5"] -> 4}]

enter image description here

Note: The reason the form EdgeLabels ->{ 1 -> 2 -> "label"} throws an error is that the expression 1 -> 2 -> "label" is parsed as 1 -> (2 -> "label") as can be seen using the FullForm of that expression:

1 -> 2 -> "label"// FullForm

Rule[1, Rule[2, "label"]]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.