From 65bfb376fa3e12dd42800c767ca47ef0c845a2bd Mon Sep 17 00:00:00 2001 From: Makithomas Date: Thu, 22 Oct 2015 15:58:07 +0900 Subject: [PATCH] test --- Maki_Exercise5.ipynb | 154 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 Maki_Exercise5.ipynb diff --git a/Maki_Exercise5.ipynb b/Maki_Exercise5.ipynb new file mode 100644 index 0000000..f5a3806 --- /dev/null +++ b/Maki_Exercise5.ipynb @@ -0,0 +1,154 @@ +{ + "cells": [ + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "HW(3): Directed vs Undirected graphs \n", + " http://web.cecs.pdx.edu/~sheard/course/Cs163/Doc/Graphs.html" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "HW(3): Adjacency lists vs matrices \n", + " http://xlinux.nist.gov/dads/HTML/adjacencyListRep.html" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "HW(3):Implement a function taht creates a fully connected graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "graph = {'A': ['B', 'C','D','E','F'],\n", + " 'B': ['A','C','D','E','F'],\n", + " 'C': ['A','B','D','E','F'],\n", + " 'D': ['A','B','C','E','F'],\n", + " 'E': ['A','B','C','D','F'],\n", + " 'F': ['A','B','C','D','E']}\n", + "def find_path(graph, start, end, path=[]):\n", + " path = path + [start]\n", + " if start == end:\n", + " return path\n", + " if not graph.has_key(start):\n", + " return None\n", + " for node in graph[start]:\n", + " if node not in path:\n", + " newpath = find_path(graph, node, end, path)\n", + " if newpath: return newpath\n", + " return None\n", + "print graph\n", + "print find_path(graph,'A','F')\n", + "def find_all_paths(graph, start, end, path=[]):\n", + " path = path + [start]\n", + " if start == end:\n", + " return [path]\n", + " if not graph.has_key(start):\n", + " return []\n", + " paths = []\n", + " for node in graph[start]:\n", + " if node not in path:\n", + " newpaths = find_all_paths(graph, node, end, path)\n", + " for newpath in newpaths:\n", + " paths.append(newpath)\n", + " return paths\n", + "find_all_paths(graph, 'A', 'D')\n", + " " + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "HW(3): Implement a function that creates a randomly connected graph" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "V = {1:'A',2:'B',3:'C',4:'D',5:'E',6:'F'} # number of verticies : n = 6\n", + "# I am going to generate DIRECTED random graphs by using a 'random' command and dictionaries. Because the number of edges and the end verticy(ies) from a verticy has no correlation to others, it is simply a linear relationship.\n", + "\n", + "import random\n", + "from random import randint\n", + "import string\n", + "def id_generator(size, chars=string.ascii_uppercase):\n", + " return \"\".join(random.sample(chars,size))\n", + "graph = {}\n", + "i = 0\n", + "v = \"ABCDEF\"\n", + "for i in V:\n", + " e = (randint(0,5))#For n numbers of verticy, number of edge(s) :e extends from each verticy is randomly selected from 0 to n-1\n", + " ev = v.replace(V[i],\"\") #combinatioin of verticies that have possibility to be connected with the start-verticy\n", + " print e, ev\n", + " graph[V[i]] = list(id_generator(e,ev))\n", + " i +=1\n", + "print graph\n", + "#Every run randomly generates a different graph.\n", + "\n", + "def find_path(graph, start, end, path=[]):\n", + " path = path + [start]\n", + " if start == end:\n", + " return path\n", + " if not graph.has_key(start):\n", + " return None\n", + " for node in graph[start]:\n", + " if node not in path:\n", + " newpath = find_path(graph, node, end, path)\n", + " if newpath: return newpath\n", + " return None\n", + "find_path(graph,'A','F')\n", + "def find_all_paths(graph, start, end, path=[]):\n", + " path = path + [start]\n", + " if start == end:\n", + " return [path]\n", + " if not graph.has_key(start):\n", + " return []\n", + " paths = []\n", + " for node in graph[start]:\n", + " if node not in path:\n", + " newpaths = find_all_paths(graph, node, end, path)\n", + " for newpath in newpaths:\n", + " paths.append(newpath)\n", + " return paths" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}