Skip to content

Instantly share code, notes, and snippets.

@umgefahren
Created December 19, 2021 14:05
Show Gist options
  • Save umgefahren/75bc75841e97f7f8807c15c33a542e41 to your computer and use it in GitHub Desktop.
Save umgefahren/75bc75841e97f7f8807c15c33a542e41 to your computer and use it in GitHub Desktop.
Graph Viz
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Graph Viz",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOe7enuArzT3aHjNy/SQ5DM",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/umgefahren/75bc75841e97f7f8807c15c33a542e41/graph-viz.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "584YspdI8Lub",
"outputId": "671ac7e4-5cba-4150-8ee8-69dc04a3d5f9"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: graphviz in /usr/local/lib/python3.7/dist-packages (0.10.1)\n"
]
}
],
"source": [
"!pip3 install graphviz"
]
},
{
"cell_type": "code",
"source": [
"import graphviz"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Ybny-3hF8PmG",
"outputId": "16e93b78-8004-46ac-80da-2b5a2d10bf44"
},
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('0.10.1', (2, 40, 1))"
]
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "code",
"source": [
"file = open(\"large.in\")\n",
"\n",
"dot = graphviz.Digraph(comment=\"Graph\")\n",
"\n",
"lines = file.read().split(\"\\n\")\n",
"\n",
"for x in range(len(lines)):\n",
" if x == 0:\n",
" continue\n",
" splited = lines[x].split(\" \")\n",
" if len(splited) == 2:\n",
" start, end = splited[0], splited[1]\n",
" dot.edge(str(end), str(start), dir=\"none\")\n",
" elif len(splited) == 1:\n",
" continue\n",
" else:\n",
" start, end, weight = splited[0], splited[1], splited[2]\n",
" dot.edge(str(start), str(end), str(weight), dir=\"none\")\n",
"\n",
"dot\n",
" "
],
"metadata": {
"id": "CGVyLYwq8RpK"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "ZgxyAQ2Y8TO6"
},
"execution_count": null,
"outputs": []
}
]
}
@umgefahren
Copy link
Author

A tool to visualize the graphs from the A&D lectures at ETH Zürich.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment