Sunday, August 14, 2016

Graphing Building a Snowman

Building a Snowman

When I was in college social networks were a big buzz word. I think the term has died down a bit, but the idea of being able to map out our relationships with others is still a pretty interesting concept. Graphing nodes and edges can also be applied to map out complex problems. I wanted to get some experience graphing a social network and decided it might be interesting to graph a time series process through a network. I found this site from Katherine Ognyanova very helpful in learning the basics of using iGraph, an R package for network analysis.

library('igraph')
## Warning: package 'igraph' was built under R version 3.2.5
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library('RColorBrewer')
nodes <- read.csv("snowmanNodes.csv")
links <- read.csv("snowmanEdges.csv")

For this example I decided to make up an example of making a snowman. Given that we’re having one of our hotter weeks of the summer in Madison, dreaming of cold weather seems fitting. It’s also something that I have years of experience having grown up in the UP. I created a few nodes for the steps involved in creating the snowman, and edges for the order of the processes and the time it takes to go from one process to another. There are two workers elves, who need to build the three parts of the snowman to assemble it.

print(nodes)
##     id             event event.type event.label
## 1  s01           Ordered          1     Process
## 2  s08      Head started          5        Head
## 3  s09     Head finished          5        Head
## 4  s04    Bottom started          3      Bottom
## 5  s05   Bottom finished          3      Bottom
## 6  s02     Elf A arrives          2 Elf Arrival
## 7  s03     Elf B arrives          2 Elf Arrival
## 8  s06    Middle started          4      Middle
## 9  s07   Middle finished          4      Middle
## 10 s10 Assembly complete          1     Process
print(links)
##    from  to time
## 1   s01 s02   10
## 2   s02 s10   90
## 3   s01 s04   15
## 4   s04 s05   50
## 5   s05 s10   35
## 6   s01 s03   40
## 7   s03 s10   60
## 8   s01 s06   45
## 9   s06 s07   40
## 10  s07 s10   15
## 11  s01 s08   70
## 12  s08 s09   25
## 13  s09 s10    5

This gives us a graph where we can see all of the processes involved in making this snowman. We can see that we have two elves arriving, and together building the bottom, middle, and head of the snowman. However, our data also has a time component to it in the edge weight. It would be nice to rearrange this graph to factor in that piece.

We have to traverse through the nodes and edges to assign the positions of each of the nodes.

totalTime <-distances(net,v=V(net)$event=="Ordered",to=V(net)$event=="Assembly complete",weights=E(net)$time)
allPaths <- all_simple_paths(net,V(net)$event=="Ordered",to=V(net)$event=="Assembly complete")
vertCount <- vcount(net)
l <- matrix(data=NA,nrow=vcount(net),ncol=2)  #set our coordinate matrix
if (length(allPaths)%%2==1) {
  midPoint <- length(allPaths)/2 + 1
} else {
  midPoint <- length(allPaths)/2 + .5
}

l[1,1] <- midPoint
l[1,2] <- 1
l[10,1] <- midPoint
l[10,2] <- totalTime
for (i in 1:length(allPaths)) {
  pathPoints<-E(net,path=allPaths[[i]])$time
  curTime <- 0 
  for (j in 2:(length(allPaths[[i]])-1)){
    curVert <- 0
    for (k in 1:vertCount) {
      curVert <- curVert+1
      if (V(net)[curVert]==allPaths[[i]][[j]]) {
        break
      }
    }
    curTime <- pathPoints[j-1] + curTime  #find our cur Y axis
    l[curVert,1] <- i # set the x coordinate
    l[curVert,2] <- curTime  # set the y coordinate for time

  }
  
}

I rearranged the process to represent time on the y axis. It’s easier to see how long the whole process takes, as well as the individual sub steps to build the snowman. We can see that the bottom could not be started until one of the elves arrived. From there the bottom snowball was started at 15 minutes from the start, and completed 50 minutes later at 65 minutes. The middle could not start until the second elf arrived, and the head could not be started until one of the elves finished their current snowball. Once all three snowballs were complete, they could be assembled, and the snowman was finished.

plot(net,edge.arrow.size=1,vertex.label.color="black",vertex.label.dist=.6,layout=l,vertex.label.cex=2)
legend(x=-1, y=-1, unique(V(net)$event.label), pch=21,
       col="#777777", pt.bg=pal, pt.cex=4, cex=1.6, bty="n", ncol=1)
axis(side=2,at=c(-1,-.5,0,.5,1),labels=c("0","25","50","75","100"),cex.axis=2)
mtext(side=2,text="Time Minutes",cex=2,padj = -2.25)

No comments:

Post a Comment