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 3974262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:32:51+00:00 2026-05-20T04:32:51+00:00

In my graph-package (as in graph theory, nodes connected by edges) I have a

  • 0

In my graph-package (as in graph theory, nodes connected by edges) I have a vector indicating for each edge the node of origin from, a vector indicating for each edge the node of destination to and a vector indicating the curve of each edge curve.

By default I want edges to have a curve of 0 if there is only one edge between two nodes and curve of 0.2 if there are two edges between two nodes. The code that I use now is a for-loop, and it is kinda slow:

curve <- rep(0,5)
from<-c(1,2,3,3,2)
to<-c(2,3,4,2,1)

    for (i in 1:length(from))
    {
        if (any(from==to[i] & to==from[i]))
        {
            curve[i]=0.2        

        }
    }

So basically I look for each edge (one index in from and one in to) if there is any other pair in from and to that use the same nodes (numbers).

What I am looking for are two things:

  1. A way to identify if there is any pair of nodes that have two edges between them (so I can omit the loop if not)
  2. A way to speed up this loop

#

EDIT:

To make this abit clearer, another example:

from <- c(4L, 6L, 7L, 8L, 1L, 9L, 5L, 1L, 2L, 1L, 10L, 2L, 6L, 7L, 10L, 4L, 9L)
to <- c(1L, 1L, 1L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 10L, 10L)
cbind(from,to)
      from to
 [1,]    4  1
 [2,]    6  1
 [3,]    7  1
 [4,]    8  2
 [5,]    1  3
 [6,]    9  3
 [7,]    5  4
 [8,]    1  5
 [9,]    2  6
[10,]    1  7
[11,]   10  7
[12,]    2  8
[13,]    6  8
[14,]    7  8
[15,]   10  8
[16,]    4 10
[17,]    9 10

In these two vectors, pair 3 is identical to pair 10 (both 1 and 7 in different orders) and pairs 4 and 12 are identical (both 2 and 8). So I would want curve to become:

 [1,]  0.0
 [2,]  0.0
 [3,]  0.2
 [4,]  0.2
 [5,]  0.0
 [6,]  0.0
 [7,]  0.0
 [8,]  0.0
 [9,]  0.0
[10,]  0.2
[11,]  0.0
[12,]  0.2
[13,]  0.0
[14,]  0.0
[15,]  0.0
[16,]  0.0
[17,]  0.0

(as vector, I transposed twice to get row numbers).

Solution

from <- c(4L, 6L, 7L, 8L, 1L, 9L, 5L, 1L, 2L, 1L, 10L, 2L, 6L, 7L, 10L, 4L, 9L)
to <- c(1L, 1L, 1L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 10L, 10L)

srt <- apply(cbind(from,to),1,sort)
dub <- duplicated(t(srt))|duplicated(t(srt),fromLast=T)
curve <- ifelse(dub,0.2,0)

Benchmarking solutions

Here is some benchmarking of different solutions

> # for-loop
> system.time(
+ {
+ curve <- rep(0,5)
+     for (i in 1:length(from))
+     {
+         if (any(from==to[i] & to==from[i]))
+         {
+             curve[i]=0.2        
+ 
+         }
+     }
+ })
   user  system elapsed 
 171.49    0.05  171.98 

from <- sample(1:1000,100000,T)
> to <- sample(1:1000,100000,T)
> 
> # My solution:
> system.time(
+ {
+ srt <- apply(cbind(from,to),1,sort)
+ dub <- duplicated(t(srt))|duplicated(t(srt),fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
   user  system elapsed 
  16.92    0.00   16.94 
> 
> 
> # Marek 1:
> system.time(
+ {
+ srt <- cbind(pmin(from,to), pmax(from,to) )
+ dub <- duplicated(srt)|duplicated(srt,fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
   user  system elapsed 
   2.43    0.00    2.43 
> 
> # Marek 2:
> system.time(
+ {
+ srt <- cbind(ifelse(from>to,to,from),ifelse(from>to,from,to))
+ dub <- duplicated(srt)|duplicated(srt,fromLast=T)
+ curve <- ifelse(dub,0.2,0)
+ })
   user  system elapsed 
   2.67    0.00    2.70 
> 
> # Maiasaura:
> library(plyr)
> 
> system.time(
+ {
+ data=data.frame(cbind(id=1:length(from),from,to))
+ data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
+ curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
+ result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
+ result$value[which(is.na(result$value))]=0
+ result=data.frame(from,to,curve=result$value)
+ })
   user  system elapsed 
 103.43    0.11  103.95

> # Marek 1 + Joshua
> > system.time(
> + {
> + srt <- cbind(pmin(from,to), pmax(from,to) )
> + curve <- ifelse(ave(srt[,1], srt[,1], srt[,2], FUN=length) > 1,
> 0.2, 0)
> + })    user  system elapsed 
>    7.26    0.00    7.25

which gives the fastest solution being:

srt <- cbind(pmin(from,to), pmax(from,to) )
dub <- duplicated(srt)|duplicated(srt,fromLast=T)
curve <- ifelse(dub,0.2,0)
  • 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-05-20T04:32:51+00:00Added an answer on May 20, 2026 at 4:32 am

    Here is a solution using plyr

    I first combine from and to into a data.frame

    library(plyr)
    data=data.frame(cbind(id=1:length(from),from,to))
    

    data

      id from to
    1   1    4  1
    2   2    6  1
    3   3    7  1
    4   4    8  2
    5   5    1  3
    6   6    9  3
    7   7    5  4
    8   8    1  5
    9   9    2  6
    10 10    1  7
    11 11   10  7
    12 12    2  8
    13 13    6  8
    14 14    7  8
    15 15   10  8
    16 16    4 10
    17 17    9 10
    

    then the following should produce the result you seek:

    data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
    curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
    result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
    result$value[which(is.na(result$value))]=0
    result=data.frame(from,to,curve=result$value)
    

    should produce:

       from to curve
    1     4  1   0.0
    2     6  1   0.0
    3     7  1   0.2
    4     8  2   0.2
    5     1  3   0.0
    6     9  3   0.0
    7     5  4   0.0
    8     1  5   0.0
    9     2  6   0.0
    10    1  7   0.2
    11   10  7   0.0
    12    2  8   0.2
    13    6  8   0.0
    14    7  8   0.0
    15   10  8   0.0
    16    4 10   0.0
    17    9 10   0.0
    

    You can turn the above code into a function

    calculate_curve <- function (from,to)
    {
    data=data.frame(cbind(id=1:length(from),from,to))
    data=ddply(data, .(id), transform, f1=min(from,to),f2=max(from,to))
    curved=data.frame(data[which(duplicated(data[,4:5])==TRUE),],value=0.2)
    result=join(data[,4:5],curved[,4:6],by=intersect(names(data)[4:5],names(curved)[4:6]))
    result$value[which(is.na(result$value))]=0
    return (result$value)
    }
    

    and just do

    curve=calculate_curve(from,to)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DirectShow graph to render MPEG2/4 movies from a network stream. When
There is a directed graph (not necessarily connected) of which one or more nodes
So I have made this simple interface: package{ public interface GraphADT{ function addNode(newNode:Node):Boolean; }
I am using python with networkx package. I need to find the nodes connected
doesn't show specified graph in Mathematica Graph Theory package 'Combinatorica'.
In graph theory, what is the distinction between minimal distance (which Dijkstra's algorithm finds),
Given graph, how could i represent it using adj matrix?. I have read lots
I have an object graph serialized to xaml. A rough sample of what it
I have this object graph, I want to map: abstract Account (username, password, ...)
I want to create an extendible package I am writing that has Topology, Node

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.