Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

October 28, 2013

Six degrees of Kevin Bacon featuring Quickgraph

If you don't know what a Bacon Number is you can look it up here.

The basic idea is to calculate between how many movies there are between Kevin Bacon and another actor, the higher the number the further away. It is a very geeky calculation, in fact it so geeky that it is a part of google. Just google [actor name] bacon number and...


So how would you do this yourself?

First off we will need some data.

I used this to create a movie database. Basically you get an Actor table, a Dvd table and a Dvd_Actor table to link them.

Next we will just have to loop through all the records and create nodes(vertices) for each actor and movie in the database.

I used quickgraph and a sinple class to keep the nodes:

    public class MovieInformationNode
    {
        public int Id { get; set; }
        public NodeTypes NodeType { get; set; }
        public string Name { get; set; }
        public string PresentationName
        {
            get
            {

                if (NodeType == NodeTypes.Actor)
                    return "Actor: " + Name;
                else
                    return "Movie: " + Name;
            }
        }
    }

I declared the graph as:

private UndirectedGraph<MovieInformationNode, Edge<MovieInformationNode>> movieGraph                   = new UndirectedGraph<MovieInformationNode, Edge<MovieInformationNode>>();

For each Actor and Dvd I created a node and inserted it into the graph:

                MovieInformationNode node = new MovieInformationNode();
                node.NodeType = NodeTypes.Movie; //or actor if it is an actor, enum to keep track of type
                node.Id = dvd.Id;
                node.Name = dvd.DVD_Title.Trim();
                movieGraph.AddVertex(node);

For each link in Dvd_Actor I found the nodes in the graph and added an edge between them:

                //find actor
                MovieInformationNode actorNode = movieGraph.Vertices.Where(s => s.Id ==                                                               performance.Id && s.NodeType == NodeTypes.Actor).First();
                //find movie
                MovieInformationNode movieNode = movieGraph.Vertices.Where(s => s.Id ==                                                         performance.dvdid && s.NodeType == NodeTypes.Movie).First();
                Edge<MovieInformationNode> edge = new Edge<MovieInformationNode>(actorNode,                                                                               movieNode);
                movieGraph.AddEdge(edge);

Then you just need to get the shortest path:

            Func<Edge<MovieInformationNode>, double> edgeCost = (edge => 1.0D); //no weights
            var tryPath = movieGraph.ShortestPathsDijkstra(edgeCost, sourceNode);
            IEnumerable<Edge<MovieInformationNode>> path;
            if (tryPath(destinationNode, out path))
            {
                foreach (var item in path)
           {
                    listFrom.Items.Add(item.Source);
                    listFrom.Items.Add(item.Target);
           }
            }

To get this working you will need to get quickgraph here.


June 18, 2012

Philosophy, technology, religion

Sometimes programmers are strange, actually most of the time programmers are strange.

Me included.

And maybe this is just my twisted and over-simplified view on the subject, but I see a clear line between the developer with a philosophical view on the subject and those with a more technological take on it.

It is a division that can be seen all the way from the universities split in educations for system sciences and those for software engineers.

Philosophers seeks the best way to split reality into parts, engineers tries to make the best parts to resemble the world. Top-down and bottom-up. People that are specialized in abstracting and those that are experts in coding.



Both sides are needed, but problem's arise when coders forget that that is a fact and adopt a one way minded view of the subject, often with an almost religous conviction.

Give programmers a buzzword and they will follow.

The technological religions seems to dominate right now. Most of the new methods and patterns results in a bottom up approach to development. An alluring belief that following a path can create the perfect system.

There are definitely religious philosophers too, an architect colleague to me once said that "when you construct a system, the most important is to be as flexible as possible and you achieve maximum flexibility by not constructing anything.".

The scary part was that he meant it.

The good thing, that he eventually got fired.

I went to a (for me too short) seminar with Chris Klug about the problems with the Test Driven Development (TDD) paradigm and got inspired.

TDD has a religious following in that the users seem to believe that every project gets better if you follow it strictly, the code gets better and the solution gets better. Use tests everywhere and you will see the light.

Lately, I have heard the same opinions about a lot of technologies and methods and if I look back I have heard the same statements before about older technologies. Some people seem to believe that the solution of all problems in the world lies in a three letter acronym.

CQRS, TDD, Scrum, ORM, Functional programming, Object orientation, SOA, Sql, NoSql, Open Source...

There are grains and sometimes tons of truth in all methods, but in my world they are just pieces in the big jigsaw puzzle. Use it, but don't be a Taliban.

Each tool has its place and a time when it should be forgotten.

The goal is to make a good system not to use a certain method or technology.

No method will do the work for you.

Amen...