5
$\begingroup$

The elements in

e = {{6414, 6598, 6927}, {7, 1035, 2382, 6927}, …} 

are the graph nodes; for example, {6414,6598,6927} is equivalent to the two pairs, {6414, 6598} and {6598, 6927}. I wanted to know if a Graph object can take non-paired edge list format as in the original e. If not I can convert each edge element into a pair-format by using Partition, i.e. Partition[e, 2, 1].

$\endgroup$
  • $\begingroup$ @Szabolcs is correct. I now see that your matrix e is (likely) not an adjacency list. So what does your list e represent? Can you craft a teeny, illustrative example? What does 6414 in the matrix represent? $\endgroup$ – David G. Stork Jun 10 '15 at 16:47
1
$\begingroup$

I'm assuming that your matrix is a weighted adjacency matrix.

 WeightedAdjacencyGraph[mywts = Table[RandomReal[], {4}, {4}], 
 VertexLabels -> "Name",
 EdgeLabels -> 
  Table[Rule[EdgeList[mygraph][[i]], Flatten[mywts][[i]]], {i, 16}]]

enter image description here

$\endgroup$
1
$\begingroup$

Thread is useful for converting this format to a simple edge list.

Example:

Thread[ 1 <-> {2,3,4} ]

(* {1 <-> 2, 1 <-> 3, 1 <-> 4} *)

Using this idea, you could do

Graph@Catenate[Thread[First[#] <-> Rest[#]] & /@ e]

For Mathematica versions earlier than 10, use Flatten[..., 1] in place of Catenate[...].

$\endgroup$
1
$\begingroup$
e = {{6414, 6598, 6927}, {7, 1035, 2382, 6927}};

BlockMap

Graph[Join @@ (BlockMap[DirectedEdge @@ # &, #, 2, 1] & /@ e), 
    VertexShapeFunction -> "Name"]

Mathematica graphics

Partition

Using the undocumented 6-argument form of Partition:

Graph[Join @@ (Partition[#, 2, 1, {1, -1}, {}, DirectedEdge] & /@ e), 
  VertexShapeFunction -> "Name"] 

same picture

PathGraph + GraphUnion

 GraphUnion @@ (PathGraph[#, DirectedEdges -> True] & /@ e);
 SetProperty[%, VertexShapeFunction -> "Name"]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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