4
$\begingroup$

I'm trying to highlight the path between two vertices, and it seems that PathGraph along with HighlightGraph is what I should use. However, only the vertices of the path graph get highlighted. The edges between the vertices are not being highlighted.

Here's my code:

G = 
  Graph[
    {1 -> 3, 1 -> 9, 3 -> 4, 4 -> 3, 3 -> 7, 4 -> 1, 4 -> 8, 4 -> 2, 9 -> 2, 
     9 -> 10, 5 -> 9, 2 -> 5, 2 -> 7, 10 -> 2, 7 -> 10, 7 -> 8, 7 -> 2, 8 -> 6, 
     8 -> 7, 6 -> 8, 6 -> 7 }, 
    GraphStyle -> "SmallNetwork"]

I'll get the following:

enter image description here

Now for my attempt at highlighting the shortest path between vertex 1 and 5:

HighlightGraph[G, PathGraph[FindShortestPath[G, 1, 5]]]

I get the following:

enter image description here

What am I doing wrong? I'd like for the arrows leading from vertex 1 to 5 to be highlighted too.

$\endgroup$

1 Answer 1

5
$\begingroup$

Method one:

HighlightGraph[G, 
 PathGraph[FindShortestPath[G, 1, 5], DirectedEdges -> True]]

Method two:

HighlightGraph[G, Subgraph[G, FindShortestPath[G, 1, 5]]]


BTW, I strongly recommend avoiding the use of a single capital letter as a variable name. Note that all the characters in the phase "ONE DICK" are pre-defined in the System` context in Mathematica.

$\endgroup$
3
  • 2
    $\begingroup$ Method 2 is not so good. It highlights edge 5 -> 9, which is not part of the path. $\endgroup$
    – m_goldberg
    Commented Feb 24, 2017 at 2:57
  • $\begingroup$ Oh! I didn't know DirectedEdges was needed. Thank you so much. And noted on that point, I will change it. $\endgroup$ Commented Feb 24, 2017 at 2:58
  • $\begingroup$ @m_goldberg I mean this two method face to two cases.But I don't know which one the OP need. $\endgroup$
    – yode
    Commented Feb 24, 2017 at 2:59

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.