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:
class RoadAndCityGraph: Graph
class Node::<
cityName -> cn: ref String:
cn := id
display::<
...
class Edge::<
roadLength: var Integer
display::
...
addCity(nm: var String) -> n: ref Node:
:::
addRoad(from, to: ref Node, dist: var integer) -> e: ref Edge:
:::
inner(RoadAndCityGraph)
RoadAndCityGraph has the following attributes:
- A further binding of the virtual classes extended the descriptions of
NodeandEdgefromGraph. ANoderepresents a city and anEdgerepresents a road. - A method
addCityand a methodaddRoad.
The further binding of Node adds the following attributes:
- A method
cityNamewhich returns theidof the node – theidis used to represent the name of the city. - A further binding of
display, we which extends the description of display fromNode. - Note that no further binding of
addEdgeis included here.
The further binding of Edge adds the following attributes:
- A integer variable
roadLengthholding the length of the road. - A further binding of the
displaymethod.
Below we show the details of addCity and addNode:
addCity(nm: var String) -> n: ref Node:
n := addNode(nm)
addRoad(from, to: ref Node, dist: var integer) -> e: ref Edge:
e := from.addEdge(to)
e.roadLength := dist
AddCityhas the name,nm, of the city as parameter and adds aNodewithnmas argument. The newNodeis returned as the value ofaddNode.AddRoadhas the two cities being connected by the road and the distance between them as parameters:from,to, anddist.
We may now use class RoadAndCityGraph to define a region with some cities and roads. Herre we use Europe and Paris, London and Berlin:
Europe: obj RoadAndCityGraph
setUp:
n1, n2: ref Node
e: ref Edge
n1 := addCity("Paris")
n2 := addCity("London")
e := addRoad(n1,n2,291) -- 291 miles
e := addRoad(n2,n1,291)
n2 := addCity("Berlin")
e := addRoad(n1,n2,1054) -- 1054 km
Europe.setUp
The example should be self explanatory.
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 , it is show to be explicit about the units represented by numbers.
Exerises
- Shortest path. Given two cities A and B, write a method that computes the shorts route from A to B.
- Traveling salesman. For a given graph, write a method that computes the shortest path that visits/included all nodes in the graph.
(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.
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.
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.

