{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# 09. Graphs and Networks\n\nRepresent four spaces as vertices and three connections as edges.\n\n**Goal:** Complete one small workflow. Run the cells from top to bottom in a fresh kernel.\n\n**Extra dependencies:** None beyond the base environment.\n\nAll examples use metres unless stated otherwise.\n",
   "id": "tutorial-09-00"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "import os\nos.environ[\"TOPOLOGICPY_CORE_BACKEND\"] = \"pythonocc\"\n\nimport topologicpy\nfrom topologicpy.Core import Core\n\nassert topologicpy.__version__ == \"0.9.86\", \"Use TopologicPy 0.9.86 for these tutorials.\"\nassert type(Core.Backend()).__name__ == \"PythonOCCBackend\", \"Restart with the pythonocc backend.\"\nprint(\"TopologicPy\", topologicpy.__version__, \"—\", type(Core.Backend()).__name__)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-09-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Build a small network\n\nA graph records connectivity. These edges represent connections between spaces, rather than the physical walls of those spaces.\n",
   "id": "tutorial-09-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from topologicpy.Vertex import Vertex\nfrom topologicpy.TGraph import TGraph\n\nentrance = Vertex.ByCoordinates(0, 0, 0)\nhall = Vertex.ByCoordinates(2, 0, 0)\nstudio = Vertex.ByCoordinates(4, 0, 0)\noffice = Vertex.ByCoordinates(2, 2, 0)\nvertices = [entrance, hall, studio, office]\nedges = [{\"src\": 0, \"dst\": 1},\n         {\"src\": 1, \"dst\": 2},\n         {\"src\": 1, \"dst\": 3}]\ngraph = TGraph.ByVerticesEdges(vertices, edges, directed=False)\nassert isinstance(graph, TGraph)\nassert len(TGraph.Vertices(graph)) == 4\nassert len(TGraph.Edges(graph)) == 3\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-09-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Find a route\n\nVertex indices refer to the input order: entrance is 0, hall is 1, studio is 2 and office is 3. With geometric length as the edge cost, the route from entrance to office passes through the hall.\n",
   "id": "tutorial-09-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "route, cost = TGraph.ShortestPath(graph, 0, 3, edgeKey=\"length\", returnCost=True)\nprint(\"Route indices:\", route)\nprint(\"Route length:\", cost)\nassert route == [0, 1, 3]\nassert abs(cost - 4) < 0.0001\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-09-05"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Try it yourself\n\nAdd a direct connection from the entrance to the office. Does the shortest route change?\n",
   "id": "tutorial-09-06"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (TopologicPy tutorials)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
