{"id":5493,"date":"2024-06-20T12:13:27","date_gmt":"2024-06-20T10:13:27","guid":{"rendered":"https:\/\/oopm.org\/?page_id=5493"},"modified":"2024-12-13T11:28:35","modified_gmt":"2024-12-13T10:28:35","slug":"11-3-1-a-graph-representing-cities-and-roads","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=5493","title":{"rendered":"11.3.1 A graph representing cities and roads"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages5493&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages5493&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Next we describe a graph representing cities and roads between cities. This graph is represented by a class <code>RoadAndCityGraph<\/code>, which is a subclass of <code>Graph<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background has-tiny-font-size\"><code>   <strong>class<\/strong> RoadAndCityGraph: Graph \n      <strong>class<\/strong> Node::&lt; \n         cityName -&gt; cn: <strong>ref<\/strong> String:\n            cn := id\t\n         display::&lt; \n             ...\n      <strong>class<\/strong> Edge::&lt;\n         roadLength: <strong>var<\/strong> Integer\n         display::\n            ...\n      addCity(nm: <strong>var<\/strong> String) -&gt; n: <strong>ref<\/strong> Node: \n         :::\n      addRoad(from, to: <strong>ref<\/strong> Node, dist: <strong>var<\/strong> integer) -&gt; e: <strong>ref<\/strong> Edge:\n         :::\t \n      inner(RoadAndCityGraph)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RoadAndCityGraph<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A further binding of the virtual classes extended the descriptions of <code>Node<\/code> and <code>Edge<\/code> from <code>Graph<\/code>. A <code>Node<\/code> represents a city and an <code>Edge<\/code> represents a road.<\/li>\n\n\n\n<li>A method <code>addCity<\/code> and a method <code>addRoad<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The further binding of <code>Node<\/code> adds the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A method <code>cityName<\/code> which returns the <code>id<\/code> of the node &#8211; the <code>id<\/code> is used to represent the name of the city. <\/li>\n\n\n\n<li>A further binding of <code>display<\/code>, we which extends the description of display from <code>Node<\/code>.<\/li>\n\n\n\n<li>Note that no further binding of <code>addEdge<\/code> is included here.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The further binding of <code>Edge<\/code> adds the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A integer variable <code>roadLength<\/code> holding the length of the road.<\/li>\n\n\n\n<li>A further binding of the <code>display<\/code> method.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Below we show the details of <code>addCity<\/code> and <code>addNode<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background has-tiny-font-size\"><code>      addCity(nm: <strong>var<\/strong> String) -&gt; n: <strong>ref<\/strong> Node:\n         n := addNode(nm)\n      addRoad(from, to: <strong>ref<\/strong> Node, dist: <strong>var<\/strong> integer) -&gt; e: <strong>ref<\/strong> Edge:\n         e := from.addEdge(to)\n         e.roadLength := dist<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>AddCity<\/code> has the name, <code>nm<\/code>, of the city as parameter and adds a <code>Node<\/code> with <code>nm<\/code> as argument. The new <code>Node<\/code> is returned as the value of <code>addNode<\/code>.<\/li>\n\n\n\n<li><code>AddRoad<\/code> has the two cities being connected by the road and the distance between them as parameters: <code>from<\/code>, <code>to<\/code>, and <code>dist<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We may now use class <code>RoadAndCityGraph<\/code> to define a region with some cities and roads. Herre we use Europe and Paris, London and Berlin:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background has-tiny-font-size\"><code>   Europe: <strong>obj<\/strong> RoadAndCityGraph\n      setUp:\n         n1, n2: <strong>ref<\/strong> Node\n         e: <strong>ref<\/strong> Edge\n         n1 := addCity(\"Paris\")\t \n         n2 := addCity(\"London\")\n         e := addRoad(n1,n2,291) -- 291 miles\n         e := addRoad(n2,n1,291)\n         n2 := addCity(\"Berlin\")\n         e := addRoad(n1,n2,1054) -- 1054 km\n   Europe.setUp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The example should be self explanatory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note, however, as a remainder of the importance of being aware of units of quantities, we have deliberately shown the distance from Paris to London in miles and the distance from Paris to Berlin in kilometers. When you use Google Maps (at least at the location of the authors) you get the distance from Paris to London in miles and the one from Paris to Berlin in kilometers. In section <script>mkRef(\"Dimensions and units\");<\/script>, it is show to be explicit about the units represented by numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exerises<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><em>Shortest path.<\/em> Given two cities A and B, write a method that computes the shorts route from A to B.<\/li>\n\n\n\n<li><em>Traveling salesman<\/em>. For a given graph, write a method that computes the shortest path that visits\/included all nodes in the graph. <\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">(2) is called the travelling salesman since it is inspired by a salesman that needs to visit all cities in a given region and use the shortest route between them. It is due to the famous computer scientist E. Dijkstra.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These methods are not straight forward to write, and are examples of complex algorithms that may require the reader to study the topic of algorithms and data structures as mentioned in the introduction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For small graphs, simple algorithms may be usable, but for large graph, it is necessary to find a algorithms that are efficient with respect to the time it takes to compute them.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=5493\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Next we describe a graph representing cities and roads between cities. This graph is represented by a class RoadAndCityGraph, which is a subclass of Graph: RoadAndCityGraph has the following attributes: The further binding of Node adds the following attributes: The further binding of Edge adds the following attributes: Below we show the details of addCity [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5429,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5493","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5493"}],"version-history":[{"count":7,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5493\/revisions"}],"predecessor-version":[{"id":10320,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5493\/revisions\/10320"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5429"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}