1 line
50 KiB
Plaintext
1 line
50 KiB
Plaintext
|
|
{"cells":[{"metadata":{"run_control":{"marked":false}},"cell_type":"markdown","source":"# TD_10_1 - Représentation d'un graphe"},{"metadata":{"run_control":{"marked":false},"hide_input":true,"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport networkx as nx\nimport numpy as np\nimport pydot\nfrom networkx.drawing.nx_pydot import graphviz_layout\nfrom networkx.drawing.nx_agraph import to_agraph \nimport matplotlib.image as mpimg\n","execution_count":2,"outputs":[{"output_type":"stream","text":"Traceback (most recent call last):\n File \"<basthon-input-2-787db96fd013>\", line 4, in <module>\n import pydot\nModuleNotFoundError: No module named 'pydot'\n","name":"stderr"}]},{"metadata":{},"cell_type":"markdown","source":"Ce cours est une version \"jupyter notebook\" du [cours proposé par Stéphan Van Zuijlen](http://isn-icn-ljm.pagesperso-orange.fr/NSI-TLE/co/section_chapitre3.html)"},{"metadata":{},"cell_type":"markdown","source":"## Comment représenter un graphe"},{"metadata":{"run_control":{"marked":false}},"cell_type":"markdown","source":"Un graphe est caractérisé par sa matrice d’adjacence composée de 1 et de 0 selon que deux sommets sont ou ne sont pas reliés par une arête. \n \nUne façon d’encoder un graphe sous Python est d’utiliser un dictionnaire qui sera la représentation de sa matrice d’adjacence. \n \nLes clés seront les sommets du graphe et leur valeur sera la liste des sommets adjacents. \n \nPrenons par exemple ce graphe :"},{"metadata":{"run_control":{"marked":false},"hide_input":true,"cell_style":"split","trusted":true},"cell_type":"code","source":"G = nx.DiGraph([(\"a\",\"b\"), (\"a\",\"c\"), (\"b\",\"d\"), (\"c\",\"d\"),\n (\"b\",\"e\"), (\"d\",\"e\"), (\"e\",\"g\"), (\"e\",\"f\"),\n (\"f\",\"g\"), (\"g\",\"h\")])\nG.nodes[\"a\"]['pos'] = \"0,10!\"\nG.nodes[\"b\"]['pos'] = \"10,20!\"\nG.nodes[\"c\"]['pos'] = \"10,0!\"\nG.nodes[\"d\"]['pos'] = \"20,10!\"\nG.nodes[\"e\"]['pos'] = \"30,20!\"\nG.nodes[\"f\"]['pos'] = \"50,20!\"\nG.nodes[\"g\"]['pos'] = \"40,10!\"\nG.nodes[\"h\"]['pos'] = \"30,0!\"\n\n\npos = graphviz_layout(G)\n\nplt.figure(\"cycle\")\nnx.draw_networkx_nodes(G, pos, node_color=\"cyan\")\nnx.draw_networkx_labels(G, pos)\nnx.draw_networkx_edges(G, pos, edge_color='r', arrows=False)\nplt.box(False)\nplt.show()","execution_count":3,"outputs":[{"output_type":"stream","text":"Traceback (most recent call last):\n File \"<basthon-input-3-3c616d50977d>\", line 14, in <module>\n pos = graphviz_layout(G)\n ^^^^^^^^^^^^^^^\nNameError: name 'graphviz_layout' is not defined\n","name":"stderr"}]},{"metadata":{"cell_style":"split"},"cell_type":"markdown","source":"$\\begin{array}{ccccccccc}\n & \\left. \\begin{matrix} a & b & c & d & e & f & g & h\\end{matrix} \\right. \\\\\n \\begin{matrix} a\\\\\n b\\\\\n c\\\\\n d\\\\\n e\\\\\n f\\\\\n g\\\\\n h \\end{matrix} & \\left( \\begin{matrix}\n 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\\\\n 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\\\\n 0 & 1 & 1 & 0 & 1 & 0 & 0 & 0\\\\\n 0 & 1 & 0 & 1 & 0 & 1 & 1 & 0\\\\\n 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0\\\\\n 0 & 0 & 0 & 0 & 1 & 1 & 0 & 1\\\\\n 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\n\\end{matrix} \\right)\n\\end{array}$"},{"metadata":{},"cell_type":"markdown","source":"Cela donne:"},{"metadata":{"trusted":true},"cell_type":"code","source":"G = {}\nG['a'] = ['b','c']\nG['b'] = ['a','d','e']\nG['c'] = ['a','d']\nG['d'] = ['b','c','e']\nG['e'] = ['b','d','f','g']\nG['f'] = ['e','g']\nG['g'] = ['e','f','h']\nG['h'] = ['g']","execution_count":4,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Pour la matrice d’adjacence, on peut l’écrireà la main, en utilisant une liste de liste :"},{"metadata":{"trusted":true},"cell_type":"code","source":"A1=[[0, 1, 1, 0, 0, 0, 0, 0],\n [1, 0, 0, 1, 1, 0, 0, 0],\n [1, 0, 0, 1, 0, 0, 0, 0],\n [0, 1, 1, 0, 1, 0, 0, 0],\n [0, 1, 0, 1, 0, 1, 1, 0],\n [0, 0, 0, 0, 1, 0, 1, 0],\n [0, 0, 0, 0, 1, 1, 0, 1],\n [0, 0, 0, 0, 0, 0, 1, 0]]\n\nA1","execution_count"
|