Skip to content

Instantly share code, notes, and snippets.

@melvincabatuan
Created March 28, 2015 10:00
Show Gist options
  • Save melvincabatuan/de1fd4308a9280ec8fda to your computer and use it in GitHub Desktop.
Save melvincabatuan/de1fd4308a9280ec8fda to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Graph Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A graph, $G$, is a set of vertices (nodes) $V$ and a set of edges (arcs) $E$ that connect pairs of distinct nodes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$ G = \\{ V, E \\} $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ex."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
"Image('/home/cobalt/melvincabatuan.github.io/images/graph_ex.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Adjacency Set Representation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a, b, c, d, e, f, g, h = range(8)\n",
"N = [\n",
"{b, c, d, e, f}, # a\n",
"{c, e}, # b\n",
"{d}, # c\n",
"{e}, # d\n",
"{f}, # e\n",
"{c, g, h}, # f\n",
"{f, h}, # g\n",
"{f, g} # h\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"N"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Neighborhood membership"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"b in N[a]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"g in N[a]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Degree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"len(N[f])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Adjacency Lists"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a, b, c, d, e, f, g, h = range(8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"M = [\n",
"[b, c, d, e, f], # a\n",
"[c, e], # b\n",
"[d], # c\n",
"[e], # d\n",
"[f], # e\n",
"[c, g, h], # f\n",
"[f, h], # g\n",
"[f, g] # h\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"M"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Neighborhood membership"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"b in M[a]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"g in M[a]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Degree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"len(M[a])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Edge weight"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"M[a][b]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Adjacency Sets with Dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"P = {\n",
"'a': set('bcdef'),\n",
"'b': set('ce'),\n",
"'c': set('d'),\n",
"'d': set('e'),\n",
"'e': set('f'),\n",
"'f': set('cgh'),\n",
"'g': set('fh'),\n",
"'h': set('fg')\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"P"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Adjacency Matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a, b, c, d, e, f, g, h = range(8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
" # a b c d e f g h\n",
"Q = [[0,1,1,1,1,1,0,0],# a\n",
" [0,0,1,0,1,0,0,0], # b\n",
" [0,0,0,1,0,0,0,0], # c\n",
" [0,0,0,0,1,0,0,0], # d\n",
" [0,0,0,0,0,1,0,0], # e\n",
" [0,0,1,0,0,0,1,1], # f\n",
" [0,0,0,0,0,1,0,1], # g\n",
" [0,0,0,0,0,1,1,0]] # h"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Q"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Neighborhood membership"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Q[a][b]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Degree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sum(Q[f])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Weight Matrix with Infinite Weight for Missing Edges"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a, b, c, d, e, f, g, h = range(8)\n",
"inf = float('inf')\n",
"\n",
" # a b c d e f g h\n",
"W = [[ 0, 2, 1, 3, 9, 4, inf, inf], # a\n",
" [inf, 0, 4, inf, 3, inf, inf, inf], # b\n",
" [inf, inf, 0, 8, inf, inf, inf, inf], # c\n",
" [inf, inf, inf, 0, 7, inf, inf, inf], # d\n",
" [inf, inf, inf, inf, 0, 5, inf, inf], # e\n",
" [inf, inf, 2, inf, inf, 0, 2, 2], # f\n",
" [inf, inf, inf, inf, inf, 1, 0, 6], # g\n",
" [inf, inf, inf, inf, inf, 9, 8, 0]] # h\n",
"W"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Neighborhood membership"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"W[a][b] < inf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"W[c][e] < inf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Degree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sum(1 for w in W[a] if w < inf) - 1"
]
}
],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment