|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
http://www.derek-bartram.co.uk/index.cfm?PageID=kevinbacon - Online version of the Code Project Kevin Bacon Game. Please note that the game is still being populated so finding links presently is very hard (try a link from 10 to 1).
IntroductionThis article demonstrates how to use the CodeProject.dll to extract information for the Code Project Kevin Bacon game! IMPORTANT NOTE - the data retrieval if set of 5,000,000 as it should be will consume lots of Code Project's bandwidth; if you want to give the demo a whirl, please use a limited set of say 1000 users. I have included the bulk load data for the first 1000 users which you should really use instead. Please respect Code Project. The online version is being populated presently! Background"The trivia game Six Degrees of Kevin Bacon is based on a variation of the concept of the small world phenomenon and states that any actor can be linked through his or her film roles to actor Kevin Bacon. The game requires a group of players to try to connect any film actor in history to Kevin Bacon as quickly as possible and in as few links as possible. The game was played across various college campuses in the early 1990s.[citation needed] Its name is a pun on the concept of six degrees of separation. In 2007, Bacon started a charitable organization named SixDegrees.org." Kevin Bacon, in a 1994 Premiere interview for the film The River Wild, while talking about his fame and career, comments that he's worked with everybody in Hollywood or someone who's worked with them. Quote: Wikipedia - Six Degrees of Kevin Bacon Aim of the GameThe aim of the game is simple, find a pair of member id's who are linked via comments on articles but with the longest minimal chain of connections. For example user Mike Klimentiev (3045) to Michael Dunn (152) is pretty good, with a Bacon number of 3 (since there are three links in the chain).
User 230 is Tibor Blazko. Game TheoryThis article is not intended to serve as an introduction to the theories behind the Kevin Bacon game, however a summary is presented herein for completeness. Consider the link between users and comments in articles as a (non-fully) directed graph as shown below;
The Bacon Number (or rather path) is the path with the shortest number of hops between source and destination. This may be determined via Dijkstra's algorithm (see http://en.wikipedia.org/wiki/Dijkstra's_algorithm). I have used an existing .Net implementation in this version of the game, courtesy of Michael Demeersseman ( http://www.codeproject.com/KB/recipes/ShortestPathCalculation.aspx), albeit with a few minor alterations to store the artical information in Connection and removing Connection weight (as all connections have a weight of 1 in this game). Retriving Required DataTwo pieces of information are required, user articles and comments left in articles. The nodes (i.e. the end points) represent all articles from a single user and can be data mined via the ArticlesSummary class of the CodeProject.dll. The ArticlesSummary requires the member number, however a simple loop from 1 to 5,000,000 (the number of Code Project members) performs this task; counter = 1;
StreamWriter articleDataWriter = new StreamWriter(System.Environment.CurrentDirectory
+ "\\articles.blf", false);
articleDataWriter.AutoFlush = true;
while (counter < maxUsers && false)
{
ArticlesSummary summary = new ArticlesSummary(counter.ToString());
summary.update();
List<Article> articles = summary.Articles;
foreach (Article a in articles){
articleDataWriter.Write(counter + "," + a.Title + "," + a.Link + "\r");
}
if (counter % 100 == 0)
{
articleDataWriter.Flush();
Console.Write(".");
}
counter++;
}
articleDataWriter.Flush();
articleDataWriter.Close();
Console.WriteLine(".");
The results are written to a simple text file ready for bulk loading. The second piece of required information is comments left in articles by each user, i.e. the connections between nodes. A similar loop over user numbers performs this task; counter = 1;
StreamWriter commentDataWriter = new StreamWriter(System.Environment.CurrentDirectory
+ "\\comments.blf", false);
commentDataWriter.AutoFlush = true;
while (counter < maxUsers)
{
User u = new User(counter.ToString());
foreach (String article in u.ArticleComments)
{
commentDataWriter.Write(counter + "," + article + "\r");
}
if (counter % 100 == 0)
{
commentDataWriter.Flush();
Console.Write(".");
}
counter++;
}
commentDataWriter.Flush();
commentDataWriter.Close();
Console.WriteLine(".");
In a similar manor the resultant data is written to text file for bulk loading. Processing DataBefore the data may be successfully used, it is imported into a SQL Server database using the following commands. Create TablesUSE [KevinBacon] GO /****** Object: Table [dbo].[Articles] Script Date: 04/27/2008 21:03:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Articles]( [memberID] [nchar](10) NOT NULL, [articleName] [nchar](512) NOT NULL, [webLink] [nchar](128) NOT NULL ) ON [PRIMARY]; USE [KevinBacon] GO /****** Object: Table [dbo].[Comments] Script Date: 04/27/2008 21:03:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Comments]( [memberID] [nchar](10) NOT NULL, [articleName] [nchar](512) NOT NULL ) ON [PRIMARY]; Import DataFor those not familiar with the BULK INSERT command (or the BCP utility), it allows significantly faster data import into a SQL Server database table as constraints are not checked and only one command is required for multiple row insertion. Consider the case of importing comments data; there are 5,000,000 each with on average 20 comments, resulting on 100,000,000 INSERTs or 1 BULK INSERT. BULK INSERT Articles FROM 'C:\Users\Administrator\Documents\Visual Studio 2008\Projects\CodeProject\ KevinBaconDataRetrieval\bin\Debug\articles.blf' WITH ( FIELDTERMINATOR =',', ROWTERMINATOR ='\r' ); BULK INSERT Comments FROM 'C:\Users\Administrator\Documents\Visual Studio 2008\Projects\CodeProject\ KevinBaconDataRetrieval\bin\Debug\comments.blf' WITH ( FIELDTERMINATOR =',', ROWTERMINATOR ='\r' ); Note that the resultant output of data retrieval requires that the text file needs a minor alteration; the last character of the file needs to be removed (i.e. the last \n). Notepad or similar is sufficient; the process should be performed as follows;
* Note that the carat position does not appear to change, this is normal and correct. Calculating the Shortest PathThe game program proceeds along the following stages;
Get Location DataSELECT memberID FROM Comments GROUP BY memberID Get a list of the distinct member IDs Get Connection DataSELECT
Comments.memberID AS fromMemberID,
Articles.memberID AS toMemberID,
Articles.articleName AS articleName
FROM Articles, Comments
WHERE Articles.articleName = Comments.articleName
Performs a join on the Article and Comments tables. The fromMemberID and toMemberID is used as a key to a dictionary of Locations as previously generated, and a new Connection created accordingly (along with the articleName) Create Routing EngineRouteEngine engine = new RouteEngine();
engine.Connections = connectionsList;
engine.Locations = locationsList;
The routing engine is instantiated. HistoryVersion 1.0.0.0 - First build Useful Linkscodeprojectarticle.aspx (Part 1, Articles) codeprojectuser.aspx (Part 2, Users) codeprojectkevinbacon.aspx (Part 3, Kevin Bacon) Additional Licensing NotesPlease feel free to use this in your work, however please be aware that a modified The Code Project Open License (CPOL) is in use; basically it is the same as the standard license except that this code must not be used for commercial or not-for-profit commercial use without prior authorisation. Please see license.txt or license.pdf in the included source and demo files.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||