28. Superhero Degrees of Seperation: Review the code, and run it!

Activity

  • Import DegreesOfSeperation.scala from resource folder into Eclipse-Scala IDE in SparkScala folder

  • Open and take a look at DegreesOfSeperation.scala

  • At the start, we seperate each line in marvel-graph.txt into a BFS node with heroID, default distance and color of gray

Looking at the Code

    val startCharacterID = 5306 // SpiderMan
    val targetCharacterID = 14 // ADAM 3,031 (who?)
  • This is the characters we want to find the seperation between.

    // BFSData contains an array of hero ID connections, the distance, and color.
    var hitCounter:Option[Accumulator[Int]] = None

    //BFSData contains an array of hero ID connections, the distance, and color.
    type BFSData = (Array[Int]), Int, String)

    // A BFSNode has a heroID and the BFSData associated with it.
    type BFS Node = (Int, BFSData)
  • Specifying the BFSData and BFSNode

  • Specifying the hero ID and the connections from the csv data

  • Assigning the default color of WHITE and distance of 9999 to a new BFSNode

  • Unless this is the character we are starting from, the color is changed to GRAY and the distance is changed to 0

  • We are going to run the main method and iterate through 10 times through the graph to find out who is actually connected to one another

  • It actually goes to the function bfsMap to expand BFS Node into this node and its children

  • Returns a results of array that contains the many possible BFS nodes that contains the results for the targetCharacterID

  • hitCounter is defined as a global option, so the operation can reference it while calculating if the current node colour is gray.

  • If the current node colour is gray, it means it is the character we are starting from

  • Maps the nodes and returns the nodes with edges, minimum distance and darkest color

  • Remember for the starting character id we are starting with, the distance is set to 0 in bfsMap function

  • Now go ahead and run the code and you should see the output

The Results

  • So it means that our targetted character ADAM 3,031 is 2 degrees of seperations from Spiderman our starting character

Last updated