Code Monkey home page Code Monkey logo

six-deg-of-separation's Introduction

Ștefan MĂGIRESCU

MovieGraph


In designing the graph, I first read the data in a special collection struct that has the following format:

  • collection struct

    • no. of films
    • array of film structs
  • film struct

    • name of film
    • no. of actors
    • array of actors' names

I then proceeded with the graph building procedure which remained the same all throughout the 3 tasks. The graph struct is as following:

  • graph struct

    • no. of actors
    • array of pointers to actor adj lists's heads (node)
  • actor node

    • name of the actor
    • hash
    • degree of relation
    • pointer to neighbour

Graph building procedure


  • First, go through one movie at a time in the collection list ( addNode() ), and add each actor to the graph, as long as it hasnt been already added before ( alreadyInGraphList() )
  • Initialize each actor's hash with their index in the adjacency list
  • Go through one actor at a time and build their adjacency list ( adjList() ). Add whole film cast where they belong ( isInFilm(), addFilmCastToList() ), provided each actor in the cast hasn't already been added to the list ( alreadyInList() ). Retreive each actor's hash from the nodes list ( hashRetreiver() ) and assign it to the actor in the adjacency list.

All this above is encapsulated into BUILD_GRAPH() method, as to minimise number of lines of code.

Printing functions

  • printData() prints data from collection
  • printGraphNodes() - prints graph

Memory freeing functions

  • freeCollection()
  • freeGraph()

General functions:

  • ProbOne deals with exercise no. 1
  • ProbTwo deals with exercise no. 2
  • ProbThree deals with exercise no. 3
  • initQ allocates memory for root node
  • isEmpty checks if queue is empty
  • enqueue adds element to queue
  • dequeue deletes element from queue

Task no. 1:


  • Do Breadth First Traversal ( bfsForest() ) on graph and mark visited nodes on the way. When the algorithm ends, it's either because the whole graph has been traveresed, or because the graph is made out of several disconnected subgraphs (remember the number of members and the starting node)

  • If it is the latter, pick a node which hasn't been visited and start the BFT on that node. Do this until no unvisited node is left

  • Always compare the last visited production's number of members with the maximum number yet found

  • Print the largest production ( printLargestProduction() ), by performing BFT starting from the node belonging to the biggest production. Memorize all names in an array and sort it lexicographically, then print it.


Task no. 2:


  • Perform BFT ( bfs() ) on graph, starting from the first actor provided in the input
  • Keep track of the degree of proximity by adding 1 to the parentDegree provided in the struct, for each of its children
  • Stop the algorithm when it gets to the second actor provided in the input
  • Print the degree.

Task no. 3:


  • Use Tarjan's algorithm to find the bridges between all the subgraphs.

For more on how the functions work, check code comments.

six-deg-of-separation's People

Contributors

magynon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.