Created
January 24, 2018 15:55
-
-
Save mkolod/1bdb6863a5b11909591dc2f0bddaffb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from collections import defaultdict\n", | |
| "from itertools import chain" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "class Graph:\n", | |
| " \n", | |
| " def __init__(self):\n", | |
| " self.adjacency = defaultdict(list)\n", | |
| " \n", | |
| " def add_edge(self, a, b):\n", | |
| " self.adjacency[a].append(b)\n", | |
| " \n", | |
| " def __repr__(self):\n", | |
| " return \"Graph: \" + repr(dict(self.adjacency)) \n", | |
| " \n", | |
| " def topo_sort(self):\n", | |
| " \n", | |
| " def helper(v, visited, stack):\n", | |
| " if visited[v]:\n", | |
| " return\n", | |
| " visited[v] = True\n", | |
| " for i in self.adjacency[v]:\n", | |
| " helper(i, visited, stack)\n", | |
| " stack.insert(0, v)\n", | |
| " \n", | |
| " stack = []\n", | |
| " \n", | |
| " visited = list(\n", | |
| " chain(*\n", | |
| " [val for val in self.adjacency.values()] + \n", | |
| " [[key for key in self.adjacency.keys()]]\n", | |
| " )\n", | |
| " )\n", | |
| " \n", | |
| " visited = {k: False for k in visited}\n", | |
| " \n", | |
| " for k, v in visited.items():\n", | |
| " if not v:\n", | |
| " helper(k, visited, stack)\n", | |
| " \n", | |
| " return stack" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Topologically sort the graph below:\n", | |
| "\n", | |
| "" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "g = Graph()\n", | |
| "g.add_edge(5, 2)\n", | |
| "g.add_edge(5, 0)\n", | |
| "g.add_edge(4, 0)\n", | |
| "g.add_edge(4, 1)\n", | |
| "g.add_edge(2, 3)\n", | |
| "g.add_edge(3, 1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "Graph: {5: [2, 0], 4: [0, 1], 2: [3], 3: [1]}" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "g" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[4, 5, 0, 2, 3, 1]" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "g.topo_sort()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Note**: The above is just one of the possible topological sorts\n", | |
| "Here are all the possibilities for this graph:\n", | |
| "\n", | |
| " 4 5 0 2 3 1 \n", | |
| " 4 5 2 0 3 1 \n", | |
| " 4 5 2 3 0 1 \n", | |
| " 4 5 2 3 1 0 \n", | |
| " 5 2 3 4 0 1 \n", | |
| " 5 2 3 4 1 0 \n", | |
| " 5 2 4 0 3 1 \n", | |
| " 5 2 4 3 0 1 \n", | |
| " 5 2 4 3 1 0 \n", | |
| " 5 4 0 2 3 1 \n", | |
| " 5 4 2 0 3 1 \n", | |
| " 5 4 2 3 0 1 \n", | |
| " 5 4 2 3 1 0 " | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.6.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment