Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:48:55+00:00 2026-06-12T06:48:55+00:00

I have the following code to plot the minimum spanning tree of a graph

  • 0

I have the following code to plot the minimum spanning tree of a graph

## g is an igraph graph
mst = minimum.spanning.tree(g)
E(g)$color <- "SkyBlue2"

## how to I make mst a different color
E(g)[E(mst)]$color = "red"  ### <---- I WANT TO DO ESSENTIALLY THIS

plot(g,  edge.label=E(g)$weight)

That is, for a simple graph, I find the mst. I want to change the mst to red and plot the mst as part of the main graph. To do this, I want to select the edges of g that are also in mst. How do I do this?


UPDATE:

More generally, I have a graph g0 which is the mst of g, which has n vertices. It was constructed as follows

## implementing the Dijkstra-Prim algorithm
v0 = sample(1:n, 1)
g0 = graph.empty(n=n, directed=FALSE)
weight.g0 = 0
while(length(setdiff(1:n, v0) > 0)) {
  ## chose the shortest edge in the cut set of g

  ## to find the cut, figure out the set of edges where vertex is
  ## in v0 and the other is not
  cutset = E(g)[ v0 %->% setdiff(1:n, v0)]

  ## find the lightest weight edge
  cutweights = E(g)$weight[cutset]
  lightest_edge_idx = which(cutweights == min(cutweights))[1]
  weight.g0 = weight.g0 + min(cutweights)

  ## get the vertices of the lightest weight edge, add to path
  lightest_edge = cutset[as.numeric(cutset)[lightest_edge_idx]]
  vertices = get.edges(g, as.numeric(lightest_edge))

  g0 <- add.edges(g0, vertices, weight=min(cutweights))


  ## now that we have the vertices, add the one that is not in the
  ## graph already
  for(vtx in vertices) {
    if(!(vtx %in% v0)) {
      v0 = c(vtx, v0)
    }
  }

} 

I know I am probably not using a lot of useful features of igraph, but I do get g0 to be a mst at the end of this loop. Given this, I have

E(g0)
Edge sequence:

[1]   8 --  1
[2]   2 --  1
[3]   9 --  8
[4]   9 --  5
[5]   3 --  2
[6]   4 --  3
[7]   7 --  3
[8]  11 --  4
[9]   7 --  6
[10] 11 -- 10
> E(g)
Edge sequence:

[1]   2 --  1
[2]   5 --  1
[3]   8 --  1
[4]   3 --  2
[5]   5 --  2
[6]   6 --  2
[7]   4 --  3
[8]   6 --  3
[9]   7 --  3
[10]  7 --  4
[11] 11 --  4
[12]  6 --  5
[13]  8 --  5
[14]  9 --  5
[15]  7 --  6
[16]  9 --  6
[17] 10 --  6
[18] 10 --  7
[19] 11 --  7
[20]  9 --  8
[21] 10 --  9
[22] 11 -- 10

My question was, how do I assign an attribute to the edges in E(g) that are also in E(g0)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:48:56+00:00Added an answer on June 12, 2026 at 6:48 am

    This is actually quite easy because minimum.spanning.tree() keeps edge attributes. So you just need to assign an edge id attribute, and you’ll see which edges to color red. It goes like this:

    # Some test data, no edge weights, quite boring
    g <- erdos.renyi.game(20,2/20)
    g
    # IGRAPH U--- 20 24 -- Erdos renyi (gnp) graph
    # + attr: name (g/c), type (g/c), loops (g/l), p (g/n)
    
    E(g)$id <- seq_len(ecount(g))
    mst <- minimum.spanning.tree(g)
    mst
    # IGRAPH U--- 20 18 -- Erdos renyi (gnp) graph
    # + attr: name (g/c), type (g/c), loops (g/l), p (g/n), id (e/n)
    E(mst)$id
    # [1]  1  2  3  6  7  8  9 10 11 12 13 16 18 19 20 22 23 24
    
    E(g)$color <- "black"
    E(g)$color[E(mst)$id] <- "red"
    plot(g)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which lets the user plot two points on a
I have the following code to plot one graphic: plot(softmax(:,1), softmax(:,2), 'b.') and then
i have the following code: I=0; L=0; for i=1:20 m=[I;L]; hold on plot(1:20,m(1:2),'*'); I=I+1;
I have the following Python code which I am using to plot a filled
I have the following code to generate me a 2D plot or 2 normal
I have the following code for producing a plot of data that extends two
I have write following code to show bar plot annotation. Annotations are shown but
Say I have the following Matlab code: figure; a=plot(1:10); %A pause(); set(a,'ydata',1:2:20); %B pause();
I am using the following code for drawing a plot on a graph in
I have the following code to save a plot to a graphics format file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.