Getting a Graph's shortest path between two vertices using BFS

>> Thursday, June 9, 2011

Long title indeed, but this is a rather specific entry. I've been working with graphs lately, for university, and I'm afraid there are lots and lots of different properties and algorithms (you can imagine studying that for an exam is a pain in the ass) but well, I tried searching for this on Google with no luck, there are many algorithms for getting the shortest path between two vertex, and many sites say "It can be done using BFS" but they do not explain how.
So, I found a rather nice and easy solution. First of all, let's say G is a simple graph, with no directed edges, and we don't consider edges' weight. I'll use pseudo-code in this entry since it's more CS oriented.
First of all, if you don't know what BFS is, it's Breadth First Search, it's a way of iterate through a graph, in which you go by levels.

BFS Animation

You can see wikipedia for more info, I'll assume you know the algorithm, at least just on paper, the algorithm is like this:

Input: a vertex called start
Let Q be a queue
Q.enqueue(start)
visit start
while(Q is not empty)
    w = Q.dequeue()
    for each adyacent vertex v of w do
        if v is not visited
            mark v as visited
            add v to queue
            visit v

That's it. Basically, when you solve a problem using an algorithm like this, what you want to work on is on the "visit v" part, what we are gonna do is use a map, in which we will save each vertex we visit, and once we reached our end vertex, we simply "go back". A map is a data structure which has a Key and a Value, so we will use the Key as our vertex, and the value will be the vertex' parent, that way we can just go back and return a list of visited vertex' which make the shortest path from START to END.


Input: a vertex called start, and a vertex called end
Output: a list containing the shortest path from start to end, or null if it cannot find a path
Let Q be a queue
Let M be a map
Q.enqueue(start)
map.put(start, null) // Start has no parent!
while(Q is not empty)
    w = Q.dequeue()
    for each adyacent vertex v of w do
        if v is not visited
            mark v as visited
            add v to queue
            if(v == end)
                Let L be a list
                while(v != null)
                    L.addFirst(ww)
                    ww = M.get(ww)
                return L
return null

There it is, hopefully someone will find it useful! Note that basically the only part that changes is the 'visit' part.
I'll leave the Java code just for the sake of it ;) Sorry it's in spanish, as it's from university, but this should not be much problem.

Read more...

Fedora + Xfce on my netbook

>> Saturday, April 2, 2011

I have to admit, I'm suprised with the compatibility and the performance and stability of Fedora on my Netbook, it works even nicer than Ubuntu (or Xubuntu), I'm pretty happy with it :)
I had no major problems getting up and running even though this is the first time I try another distro than Ubuntu or any Ubuntu variation.

Here's an screenshot of my little netbook screen right now

I still have to add some more customization and programs but it's looking lovely so far :)
Definetely reccommended for netbooks, works awesome with Intel Atom processor.

Read more...

About akaikiwi and stuff

>> Monday, March 28, 2011

As I've now started University I have less time for hobby coding, I'm working on some Java stuff now, as that's what I use at university, I'm working with slick and making some game development, not a full game though, just things like GUI and stuff, once I put together some stuff I'll finish a RPG I was working on :)
About akaikiwi, it's development has drastically stopped, but I plan on finishing it, not now, but soon, I have to do some PHP for work but all Joomla! related (which to be honest I pretty much hate Joomla! awful implementation of MVC but oh well...).
I'd really love to have more time and see more RoR and Django but right now I can't, I want to finish akaikiwi first, although I do take some ideas from those frameworks, I'm really interested in Django on Appengine, I think I'll deploy my next web app there, which is probably gonna be a designer's resource site.
Once I have more free time I'll work on some nice stuff I have on hiatus :)

Read more...

NoSQL Request Injection Attacks with PHP Magic Variables

>> Monday, March 7, 2011

A "new" and interesting injection in PHP has been pointed out, and although very easy to defend against, it's pretty interesting. You can read more about it on the php website.

Read more...

Blender Journey i

>> Thursday, March 3, 2011

Blender is one of the best examples of open source software, and also one of the best 3D tools out there, with it's fame of beeing hard to use, I decided to use that excelent piece of software to learn to create 3D models.
The reason is mostly because is free, it's lightweight, it has lots of free documentation and good support and its used by professionals.

As an indie game developer I've done some nice games, I finished a few and started a lot, I have experience with 2D but I really want to try with 3D, and knowing how to use a 3D program and knowing 3D jargon will make my life way easier, plus I like to make my own resources for my games :)
The idea is using blender to make models, then I'll use Unity to create games.

I'll be reporting on my Blender journey here, I'll be using this free book, right now I read the first few chapters, until the simple man with a hat, which I made and looked funny, but similar to the one in the book :)

Read more...

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP