Giter VIP home page Giter VIP logo

bacon's Introduction

Problem Description

The goal of this project was to write a program that can take any page on Wikipedia and compute the number of links to the Wikipedia page for actor Kevin Bacon.

For example, there is a one-link separation between Footloose and Kevin Bacon, a two-link separation between Tom Cruise and Kevin Bacon, and a three-link separation between Kevin Bacon and the Taj Mahal.

Inputs to the program are a valid English Wikipedia article URL, and the output is an integer representing the degree of separation between the given URL and the Kevin Bacon Wikipedia page. The program returns -1 in the rare case when there is no such series of links.

Methodology

To search for the shortest connection the program uses breadth-first search. The program relies on using the Python Wikipedia API for requesting pages and their links from Wikipedia. Due to the slow nature of waiting on API requests, the breadth-first search algorithm is executed in parallel on multiple threads.

Since all links of interest exist on a path towards Kevin Bacon, it made sense to build a simple database of backlinks from Kevin Bacon. The program build_db.py uses a form of iterative-deepening depth-first search to discover backlinks spreading out from Kevin Bacon, to a default degree of 3. The database takes the form of a "glorified dictionary" holding only article titles and their degree of separation from Kevin Bacon.

When the main bacon.py program encounters an article outside of the database it runs the aforementioned breadth-first search until it finds one in the database. It is very rare a search is 6 or more degrees away, and the average search is around 3 degrees which is already stored in the database.

The database is relatively small at under 20MB, even though it takes a long time to build. The build_db.py is not optimized well, and could in future use a methodology similar to the parallelization in bacon.py. The alternative of downloading and parsing the latest Wikipedia database (at a whopping 8GB and up, compressed) seemed like overkill when 3 degrees and under is common for most articles.

Performance

The program relies on parsing links in a queue in memory, so large searches may exhaust all memory, given enough time. Breadth-first search is not the fastest search algorithm, but it is complete in finding the shortest path. On my machine with an Intel i5-8250 and 8GB of memory I can execute the following searches.

Title Degrees Time
Amandeep Drall Orphaned article -1 0.559s
A Few Good Men 1 0.454s
The Color of Money 2 0.373s
Communist Part of Great Britain 3 0.413s
Peanut butter 4 1.029s
Lion Express 5 1.096s

Requirements

This program was written for Python 3.9.5 and uses the wikipedia-api Python package.

Usage

Run the included Makefile to download the required Python packages.

To run a search execute the bacon.py file followed by a valid Wikipedia URL.

$ python3 bacon.py https://en.wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon

To generate a new database, or update an existing simply run the build_db.py file.

bacon's People

Watchers

James Cloos avatar Adam Peterson 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.