Sitemap
Steven Tang

Steven Tang

Finding All Unique Paths

3 min readOct 8, 2013

--

The Problem

I came across an interesting computational problem recently that I find to be a surprisingly good metaphor for what my life has become in the past few weeks. Here’s the problem:

A robot wants to travel from the top left corner of a 5x5 grid to the lower right corner. If it can move in all four directions, how many unique, non-intersecting paths exist?

I have some rudimentary experience with programming and algorithms from my high school days, and though I’ve been furiously learning about web development tools recently, I’m by no means a computer science guy (yet). And so I spent the better part of a day trying to revisit all the things about combinatorics I’d forgotten. At the end of this effort, I had learned that if the robot could only go right and down, then I could reduce the problem to the number of ways to choose 4 down moves out of 8 total moves (8C4 = 70). Still awake? Don’t worry, I have more to tell you.

Get Steven Tang’s stories in your inbox

Join Medium for free to get updates from this writer.

Turns out that I’d been trying to count all possible self-avoiding walks on a grid, which according to Wikipedia, is “conjectured to be an NP-hard problem”. Now I’m not entirely sure what that means, but I was pretty certain I wasn’t going to be able to count my way to a solution on paper. It was time to put my two weeks of Python training to the test.

Some self-avoiding walks for your viewing pleasure

Diving to the depths of my AP CompSci days, I found fuzzy images of nodes and trees and bubble sorts. As my brain tried to process this mess, a revelation came to me: treating each square on the grid as a node and treating connections between two neighboring squares as vertices, I could transform all possible paths on this grid into a graph. Now I just had to write an algorithm to find all unique paths from one node of the graph to another. No sweat. And by that, I mean a lot of sweat, tears, and Harlem Shake videos.

Sorry to disappoint you, but I’m not going to tell you the solution I eventually found because I believe everyone should go through this journey of self-discovery and computational incompetence that I experienced (and so the companies that use this as an interview question won’t yell at me). What I can say is that I couldn’t have relied on my own intellect to come to the answer. I’m lucky to live with some very talented hackers and I questioned them relentlessly. I read Stack Overflow until I saw their logo in my dreams. And it was only after a few arduous days of self-doubt and learning from collective knowledge that I succeeded.

The Robot and I

Why do I claim that this robot’s problem bears a likeness to my own?

I like to think that by moving to the Bay, I’m giving myself the chance to enumerate the possible paths I can go down in my own journey. Right now I’m at that awkward stage where I’m still trying to count it all out by hand, and while it’s amusing to think of myself as that robot who’s trying to find his way to the corner of the grid, I’m actually still figuring out what that corner I’m aiming for looks like. Luckily, I think the process of finding it will teach me a lot and introduce me to a lot of interesting people along the way. In some respects, I hope I never find out where the corner of that grid is.

Success in solving this problem is also reflective of how I think a lot of problems are solved out here — by working hard to figure out things you’re unfamiliar with and piecing together knowledge from a community of very intelligent people who are willing to help. I’m sure that if I want to call my transition to the Bay a “success,” I’ll have to do just that.

This was written on Feb 27,2013, when I first moved to the Bay.

--

--