An Algorithmic Framework for Labeling Network MapsA preliminary version of this paper has appeared in Proc. 21st Int. Conf. on Computing Combinatorics (COCOON’15), volume 9198 of Lect. Notes Comput. Sci., pages 689–700, Springer-Verlag. This research was initiated during Dagstuhl Seminar 13151 “Drawing Graphs and Maps with Curves” in April 2013

# An Algorithmic Framework for Labeling Network Maps††thanks: A preliminary version of this paper has appeared in Proc. 21st Int. Conf. on Computing Combinatorics (COCOON’15), volume 9198 of Lect. Notes Comput. Sci., pages 689–700, Springer-Verlag. This research was initiated during Dagstuhl Seminar 13151 “Drawing Graphs and Maps with Curves” in April 2013

Jan-Henrik Haunert University of Osnabrück, Germany    Benjamin Niedermann Karlsruhe Institute of Technology, Germany
###### Abstract

Drawing network maps automatically comprises two challenging steps, namely laying out the map and placing non-overlapping labels. In this paper we tackle the problem of labeling an already existing network map considering the application of metro maps. We present a flexible and versatile labeling model that subsumes different labeling styles. We show that labeling a single line of the network is NP-hard, even if we make very restricting assumptions about the labeling style that is used with this model. For a restricted variant of that model, we then introduce an efficient algorithm that optimally labels a single line with respect to a given cost function. Based on that algorithm, we present a general and sophisticated workflow for multiple metro lines, which is experimentally evaluated on real-world metro maps.

## 1 Introduction

Label placement and geographic network visualization are classical problems in cartography, which independently of each other have received the attention of computer scientists. Label placement usually deals with annotating point, line or area features of interest in a map with text labels such that the associations between the features and the labels are clear and the map is kept legible [8]. Geographic network visualization, on the other hand, often aims at a geometrically distorted representation of reality that allows information about connectivity, travel times, and required navigation actions to be retrieved easily. Computing a good network visualization is thus related to finding a layout of a graph with certain favorable properties [15]. For example, to avoid visual clutter in metro maps, an octilinear graph layout is often chosen, in which the orientation of each edge is a multiple of 45 [11, 12, 14]. Alternatively, one may choose a curvilinear graph layout, that is, to display the metro lines as curves [5, 13].

Computing a graph layout for a metro map and labeling the stops have been considered as two different problems that can be solved in succession [14], but also integrated solutions have been suggested [11, 12]. Nevertheless, in practice, metro maps are often drawn manually by cartographers or designers, as the existing algorithms do not achieve results of sufficient quality in adequate time. For example, Nöllenburg and Wolff [11] report that their method needed 10 hours and 31 minutes to compute a labeled metro map of Sydney that they present in their article, while an unlabeled map for the same instance was obtained after 23 minutes—both results were obtained without proof of optimality but with similar optimality gaps. On the other hand Wang and Chi [14] present an algorithm that creates the graph layout and labeling within one second, but they cannot guarantee that labels do not overlap each other or the metro lines.

An integrated approach to computing a graph layout and labeling the stops allows consideration to be given to all quality criteria of the final visualization. On the other hand, treating both problems separately will probably reduce computation time. Moreover, we consider the labeling of a metro map as an interesting problem on its own, since, in some situations, the layout of the network is given as part of the input and must not be changed. In a semi-automatic workflow, for example, a cartographer may want to draw or alter a graph layout manually before using an automatic method to place labels, probably to test multiple different labeling styles with the drawing. Hence, a labeling algorithm is needed that is rather flexible in dealing with different labeling styles.

In this paper, we are given the layout of a metro map consisting of several metro lines on which stops (also called stations) are located. For each stop we are further given its name, which should be placed close to its position. We first introduce a versatile and general model for labeling metro maps; see Section 2. Like many labeling algorithms for point sets [1, 3, 6], our algorithm uses a discrete set of candidate labels for each point. Often, each label is represented by a rectangle wrapping the text. Since we also want to use curved labels, however, we represent a label by a simple polygon that approximates a fat curve, that is, a curve of certain width reflecting the text height. We then prove that even in that simple model labeling a single metro line is NP-hard considering different labeling styles. Hence, we restrict the set of candidates satisfying certain properties, which allows us to solve the problem on one metro line  in  time, where  is the number of stops of ; see Section 4. This algorithm optimizes the labeling with respect to a cost function that is based on Imhof’s [8] classical criteria of cartographic quality. Utilizing that algorithm, we present an efficient heuristic for labeling a metro map consisting of multiple metro lines; see Section 6. Our method is similar to the heuristic presented by Kakoulis and Tollis [9], in the sense that it discards some label candidates to establish a set of preconditions that allow for an efficient exact solution. Our model of quality is more general than the one of Kakoulis and Tollis, however, as it not only takes the quality of individual labels but also the quality of pairs of labels for consecutive metro stations into account. Finally, we evaluate our approach presenting experiments conducted on realistic metro maps; see Section 8. Note that “stops” on “metro lines” can refer more generally to points of interest on the lines of any kind of a network map. We address labeling styles for octilinear graph layouts and curvilinear graph layouts that use Bézier curves. The more general model behind our method, however, subsumes but is not limited to these particular styles.

## 2 Labeling Model

We assume that the metro lines are given by directed, non-self-intersecting curves in the plane described by polylines, which for example have been derived by approximating Bézier curves. We denote that set of metro lines by . Further, the stops of each metro line  are given by an ordered set  of points on  going from the beginning to the end of . For two stops  we write  if lies before . We denote the union of the stops among all metro lines by and call the pair  a metro map.

For each stop  we are further given a name that should be placed close to it. In contrast to previous work, we do not follow traditional map labeling abstracting from the given text by bounding boxes. Instead we model a label  of a stop  as a simple polygon. For example, a label could have been derived by approximating a fat curve prescribing the name of the stop; see Fig. 1. For each stop  we are given a set  of labels, which we also call candidates of . The set is denoted by .

Since “names should disturb other map content as little as possible”[8], we strictly forbid overlaps between labels and lines as well as label-label overlaps. Further, each stop must be labeled. Hence, a set  is called a labeling if (1) no two labels of  intersect each other, (2) no label  intersects any metro line , and (3) for each stop  there is exactly one label .

###### Definition 1 (MetroMapLabeling)

Given: Metro map , candidates  and cost function .

Find, if it exists: Optimal labeling  of , i.e., for any labeling .

The model allows us to create arbitrarily shaped label candidates for a metro map. In our evaluation we have considered two different labeling styles. The first style, OctilinStyle, creates for each stop a set of octilinear rectangles as label candidates; see Fig. 2. We use that style for octilinear maps. The second style, CurvedStyle, creates for each stop a set of fat Bézier curves as label candidates, which are then approximated by simple polygons; see Fig. 1. We use that style for curvilinear metro maps, in order to adapt the curvilinear style of the metro map. The basic idea is that a label perpendicularly emanates from a stop with respect to its metro line and then becomes horizontal to sustain legibility. In the following section we motivate our choice of candidates based on cartographic criteria and give detailed technical descriptions for both labeling styles.

### 2.1 Two Examples of Labeling Styles

We extracted the rules for generating label candidates from Imhof’s “general principles and requirements” for map labeling [8]. For schematic network maps, the need for “legibility” implies that we must not destroy the underlying design principle with clutter. To this end, we generate candidate labels that adhere to the schematics of the network. That is, we use straight horizontal and diagonal labels with octilinear layouts and curved labels with curvilinear layouts.

We now describe more precisely, how we defined the labeling styles CurvedStyle and OctilinStyle, which are used for curvilinear layouts and octilinear layouts, respectively.

Curvilinear Metro Maps. For CurvedStyle, assume that the given metro map is curvilinear. In order to achieve a “clear graphic association” between a label and the corresponding point , we construct the simple polygon prescribing a candidate label based on a curve (possibly a straight-line segment) that emanates from . The candidate label itself is a continuous section of that does not directly start in but at a certain configurable distance from it. We define the end of the candidate label on based on the text length and assign a non-zero width to the curve section to represent the text height. In the case that lies on a single curved line , we require that and are perpendicular in to enhance the angular resolution of the final drawing. By bending towards the horizontal direction, we avoid steep labels. We approximate  by a simple polygon consisting of a constant number of line segments.

We now describe the construction of a single candidate more specifically. For each stop  of each metro line  we create a constant number of curved labels adapting the curvilinear style of the metro map. The basic idea is that a label perpendicularly emanates from  with respect to  and then becomes horizontal to sustain legibility; see Fig. 1. Let  be the normalized normal vector of  at . Further, let and be pre-defined constants. For  we define the fat cubic Bézier curve  by the following four control points; see Fig. 1. , where , , and  if  and  otherwise. We define the thickness of  to be the pre-defined height of a label. Let  be the sub-curve of  that starts at and has the length of the name of  and let  be the curve when mirroring  at . Further, let  be the length of the longest name of a stop in  and let . If  has an orientation less than or equal to , we set  and otherwise . Hence, if  is almost vertical and  is therefore almost horizontal at , we also add the labels  pointing into the opposite -direction than . In our experiments we did not let the labels start at , but with a certain offset to , in order to avoid intersections with .

Octilinear Metro Maps. For OctilinStyle assume that the metro map is octilinear. We model the labels as horizontal and diagonal rectangles. Let  be the line segment of  on which  lies and let  be an axis-aligned rectangle that is the bounding box of the name of . Further, let  be a circle around  with a pre-defined radius. We place the labels such that they touch the border of , but they do not intersect the interior of . Hence, the labels have a pre-defined offset to .

If  is horizontal, we place five copies  of  above  as follows; see Fig. 2. We place , and such that the left-bottom corner of , the midpoint of ’s bottom edge and the right-bottom corner of  coincides with the topmost point of . We rotate  by  counterclockwise and place it at  such that the midpoint of its left side touches , i.e., that midpoint lies on a diagonal through . Finally, is obtained by mirroring at the vertical line through . Mirroring  at the horizontal line through , we obtain the rectangles , respectively. We then set .

If  is diagonal, we create the candidates in the same manner as in the case that  is horizontal; see Fig. 2. However, we only create the candidates  and and the candidates that are horizontally aligned.

If  is vertical, we place three copies  of  to the right of  as follows; see Fig. 2. We rotate  by counterclockwise and by clockwise. We place , , at  such that the midpoints of their left edges touch . Mirroring , , at the vertical line through , defines the rectangles , and . We set .

In case that  is a crossing of two metro lines, we create the candidates differently. If  is the crossing of two diagonals, we create the candidates as shown in Fig. 2. If  is the crossing of a horizontal and a vertical segment, we create the labels as shown in Fig. 2. If is the crossing of a diagonal and horizontal segment, we create the labels as shown in Fig. 2. We analogously create the labels, if is the crossing of a vertical and a diagonal segment.

Remark: If a stop lies on multiple metro lines, then we can apply similar constructions, where the labels are placed on the angle bisectors of the crossing lines.

## 3 Computational Complexity

We first study the computational complexity of MetroMapLabeling assuming that the labels are either based on OctilinStyle or CurvedStyle. In particular we show that the problem is NP-hard, if the metro map consists of only one line. The proof uses a reduction from the NP-complete problem monotone planar 3SAT[10]. Based on the given style, we create for the set  of 3SAT clauses a metro map  such that  has a labeling if and only if  is satisfiable. The proof can be easily adapted to other labeling styles. Note that the complexity of labeling points using a finite set of axis-aligned rectangular label candidates is a well-studied NP-complete problem, e.g., see [7, 6]. However, since we do not necessarily use axis-aligned rectangles as labels and since for the considered labeling styles the labels are placed along metro lines, it is not obvious how to reduce a point-feature labeling instance on an instance of MetroMapLabeling. In order to show the NP-hardness, we prove that it is NP-complete to decide whether a metro map has a labeling based on the given labeling style.

###### Theorem 3.1

MetroMapLabeling is NP-complete, if the labels are based on OctilinStyle or CurvedStyle, even if the metro map has only one metro line.

###### Proof

For the illustrations we use OctilinStyle, but the same constructions can be done based on CurvedStyle; see end of proof. We first show that the problem deciding whether has a labeling lies in NP. We first create for each stop  its candidates  based on the given labeling style. Recall that each candidate has constant size. We then guess for each stop  the label  that belongs to the desired labeling . Obviously, we can decide in polynomial time whether  is a labeling of  performing basically intersection tests.

We now perform a reduction from the NP-complete Planar Monotone 3-Sat problem [10]. Let be a Boolean formula in conjunctive normal form such that it consists of  variables and clauses and, furthermore, each clause contains at most three literals. The formula induces the graph  as follows. contains for each variable a vertex and it contains for each clause a vertex. Two vertices  and are connected by an edge if and only if represents a variable  and represents a clause , such that is contained in . We call a clause of  positive (negative) if it contains only positive (negative) literals.

The formula is an instance of Planar Monotone 3-Sat if it satisfies the following requirements.

1. is monotone, i.e., each clause is either positive or negative, and

2. the graph is planar and has a rectilinear plane embedding such that

1. the vertices representing variables are placed on a horizontal line ,

2. the vertices representing negative clauses are placed below ,

3. the vertices representing positive clauses are placed above ,

4. the edges are drawn on their respective side of .

Planar Monotone 3-Sat then asks whether  is satisfiable.

Using only stops lying on single horizontal and vertical segments, we construct a metro map  that mimics the embedding of . In particular  will only consist of one metro line  that connects all stops such that the stops and their candidates simulate the variables and clauses of . We will prove that  has a labeling if and only if is satisfiable. We refer to Fig. 3 for a sketch of the construction. We first define gadgets simulating variables, clauses and connecting structures. Each gadget consists of a set of stops that lie on the border of a simple polygon . Later on we use this polygon  to prescribe the shape of the metro line .

Chain. The chain gadget represents and transmits truth values from variables to clauses mimicking the embeddings of the edges in . A chain consists of an even number of stops  that lie on vertical and horizontal segments; see Fig. 4(a). Hence, with respect to the given labeling style each stop  has a predefined set of candidates . For each stop  there are two specially marked candidates and that lie on opposite sides of ’s segment; for an example see the filled blue labels in Fig 4(a). We say that those labels are selectable, because we define the gadget such that those labels are the only labels that can be selected for a labeling. To that end, we lay out the metro line such that it does not intersect any selectable label, but all labels that are not selectable. The stops are placed such that the following conditions are satisfied.

1. The label  intersects the label  for .

2. Except the intersections mentioned in (1), there is no intersection between selectable labels of different stops.

3. The segments of the stops are connected by polylines s.t. the result is a simple polygon  intersecting all labels except the selectable labels.

The labels  and do not intersect any selectable labels; we call them the ports of the chain. Later on, we use the ports to connect other gadgets with the chain, i.e., we arrange the gadgets such that two of their ports intersect, but no other selectable label. Further, we assign a polarization to each selectable label. The labels  are negative and the labels  are positive.

Consider a labeling  of a chain assuming that  is interpreted as a metro line; we can cut  at some point in order to obtain an open curve. By construction of  only selectable labels are contained in . In particular we observe that if the negative port  is not contained in , then the positive labels  belong to . Analogously, if the positive port  is not contained in , then the negative labels  belong to . We use this behavior to represent and transmit truth values through the chain.

Fork. The fork gadget splits an incoming chain into two outgoing chains and transmits the truth value represented by the incoming chain into the two outgoing chains. A fork consists of three stops , and such that  and are placed on vertical segments and  is placed on a horizontal segment; see Fig. 4(b). Analogously to the chain, each stop  with () has two selectable labels  and . We arrange the stops such that the following conditions are satisfied.

1. The labels  and  intersect . Apart from those two intersections no selectable label intersects any other selectable label.

2. The segments of the stops are connected by polylines s.t. the result is a simple polygon  intersecting all labels except the selectable labels.

The label  is the incoming port and the labels and are the outgoing ports of the fork. We distinguish two types of forks by assigning different polarizations to the selectable labels. In the negative (positive) fork, the labels , and are positive (negative) and the labels are , and are negative (positive). Hence, the incoming port is positive (negative) and the outgoings ports are negative (positive).

Consider a labeling  of a fork assuming that is interpreted as a metro line. By construction of  only selectable labels belong to . Further, if the incoming port  does not belong to , then the outgoing ports  and belong to . Finally, if one outgoing port does not belong to , then the incoming port belongs to .

Clause. The clause gadget represents a clause  of the given instance. It forms a chain of length 2 with the addition that it has three ports instead of two ports; see Fig 4(c). To that end one of both stops has three selectable labels; one intersecting a selectable label of the other stop, and two lying on the opposite side of the stop’s segment without intersecting any selectable label of the other stop. The gadget is placed at the position where the vertex of  is located in the drawing of ; see Fig. 3. We observe that a labeling  of a clause gadget always contains at least one port. Further, we do not assign any polarization to its selectable labels.

Variable. The variable gadget represents a single variable . It forms a composition of chains and forks that are connected by their ports; see Fig 4(d). More precisely, let be the number of clauses in which the negative literal  occurs and let  be the number of clauses in which the positive literal  occurs. Along the horizontal line  on which the vertex of  is placed in the drawing of , we place a horizontal chain . Further, we place a sequence of negative forks  to the left of  and a sequence of positive forks to the right of . The negative incoming port of  is connected to the positive port of  by a chain. Two consecutive forks  and  are connected by a chain  such that  connects a positive outgoing port of  with the negative incoming port of . Analogously, the positive incoming port of  is connected to the negative port of  by a chain. Two consecutive forks  and  are connected by a chain  such that  connects a negative outgoing port of  with the positive incoming port of .

We observe that the gadget has free ports. Further, we can arrange the forks such that the free ports of  lie above  and the free ports of  lie below .

Consider a labeling  of a variable. By construction of the forks and chains, if one positive free port is not contained in , then all negative free ports must be contained in . Analogously, if one negative free port is not contained in , then all positive free ports must be contained in .

Using additional chains we connect the positive free ports with the positive clauses and the negative ports with the negative clauses correspondingly; see Fig. 3. More precisely, assume that the variable  is contained in the positive clause ; negative clauses can be handled analogously. With respect to the drawing of , a positive free port of ’s gadget is connected with the negative port of a chain whose positive port is connected with a free port of ’s gadget. Note that we can easily choose the simple polygons enclosing the gadgets such that they do not intersect by defining them such that they surround the gadgets tightly.

One Metro Line. We construct the polygons enclosing the single gadgets such that they do not intersect each other. We now sketch how the polygons can be merged to a single simple polygon . Cutting this polygon at some point we obtain a polyline prescribing the desired metro line.

We construct a graph as follows. The polygons of the gadgets are the vertices of the graph and an edge is contained in  if and only if the corresponding gadgets of the polygons  and  are connected by their ports; see Fig. 3. Since  is planar and the gadgets mimic the embedding of , it is not hard to see that  is also planar. We construct a spanning tree  of . If an edge of  is also contained in , we merge and obtaining a new simple polygon ; see for an example Fig. 3. To that end we cut  and in polylines and connect the four end points by two new polylines such that the result is a simple polygon. We in particular ensure that the new polygon does not intersect any other polygon and that  intersects the same labels as  and together. In  we correspondingly contract the edge. Note that by contracting edges, remains a tree. We repeat that procedure until consists of a single vertex, i.e., only one simple polygon is left.

Soundness. It is not hard to see that our construction is polynomial in the size of the given 3SAT formula .

Assume that  is satisfiable. We show how to construct a labeling  of the constructed metro map. For each variable  that is true (false) in the given truth assignment, we put all negative (positive) labels of the corresponding variable gadget and its connected chains into . By construction those labels do not intersect. It remains to select labels for the clause gadgets. Consider a positive clause ; negative clauses can be handled analogously. Since  is satisfiable, contains a variable  that is true in the given truth assignment of . The set contains only negative labels of the chain connecting the gadget of  with the gadget of , but no positive labels of that chain. Hence, we can add the port of ’s gadget that is connected to that chain without creating intersections. For the second stop of the clause we put that selectable label into  that is not a port. We can apply this procedure to all positive and negative clauses without creating intersections, which yields the labeling  of the constructed metro map.

Finally, assume that we are given a labeling  of the constructed metro map. Consider the clause gadget of a positive clause ; negative clauses can be handled analogously. By construction contains at least one port  of that gadget. This port is connected to a chain, which is then connected to a gadget of a variable . We set that variable  true. We apply this procedure to all clauses; for negative clauses we set the corresponding variable to false. Since  is contained in , only negative labels of that chain can be contained in , but no positive labels. Hence, the positive ports of the variable gadget are also not contained in . By the previous reasoning this implies that all negative ports of the gadget are contained in . Consequently, by applying a similar procedure to negative clauses, it cannot happen that  is set to false. Altogether, this implies a valid truth assignment of .

Remarks: Fig. 5 illustrates the construction of the gadgets for CurvedStyle. Note that only the fork gadget, the clause gadget and the chain gadget rely on the concrete labeling style. Further, using CurvedStyle, a stop lying on a vertical segment  has exactly two different distinguish labels; one that lies to the left of  and one that lies to the right of .∎

## 4 Labeling Algorithm for a Single Metro Line

We now study the case that the given instance  consists only of one metro line . Based on cartographic criteria we introduce three additional assumptions on , which allows us to efficiently solve MetroMapLabeling.

For each stop , we assume that each candidate  is assigned to one side of ; either is a left candidate assigned to the left side of , or is a right candidate assigned to the right side of . For appropriately defined candidate sets those assignments correspond with the geometric positions of the candidates, i.e., left (right) candidates lie on the left (right) hand side of .

###### Assumption 1 (Separated Labels)

Candidates that are assigned to different sides of  do not intersect.

This assumption is normally not a real restriction, because for appropriately defined candidate sets and realistic metro lines, the line  separates both types of candidates geometrically. We further require what we call the transitivity property.

###### Assumption 2 (Transitivity Property)

For any three stops  with  and any three candidates , and assigned to the same side of , it holds that if neither and intersect nor and intersect then also and do not intersect; see also Fig. 6(b)–(c).

In our experiments we established Assumption 1 and Assumption 2 by removing candidates greedily. In Section 8 we show that for real-world metro maps and the considered candidate sets we remove only few labels, which indicates that those assumptions have only a little influence on the labelings.

Two stops  with  are consecutive if there is no other stop  with ; see Fig. 6(a). For two consecutive stops  we say that each two candidates  and  are consecutive and denote the set that contains each pair of consecutive labels in  by . Further, two consecutive labels  form a switchover  if they are assigned to opposite sides of , where denotes an ordered set indicating the order of the stops of  and . Two switchovers of  are consecutive in  if there is no switchover in  in between of both. We define the set of all switchovers in  by  and the set of consecutive switchovers in by .

Based on cartographic criteria extracted from Imhof’s “general principles and requirements” for map labeling [8], we require a cost function of the following form; see also Section 5 for a detailed motivation of .

###### Assumption 3 (Linear Cost Function)

For any  we require

 w(L)=∑ℓ∈Lw1(ℓ)+\smashoperator[r]∑(ℓ1,ℓ2)∈PLw2(ℓ1,ℓ2)+\smashoperator[r]∑(σ1,σ2)∈ΓLw3(σ1,σ2),

where  rates a single label, rates two consecutive labels and rates two consecutive switchovers.

In particular, we define  such that it penalizes the following structures to sustain readbility. (1) Steep or highly curved labels. (2) Consecutive labels that lie on different sides of , or that are shaped differently. (3) Consecutive switchovers that are placed close to each other.

If  satisfies Assumption 13, we call MetroMapLabeling also SoftMetroLineLabeling. We now introduce an algorithm that solves this problem in  time, where  and . Note that  is typically constant. We assume w.l.o.g. that  contains only candidates that do not intersect .

#### Labels on One Side.

We first assume that all candidate labels in are assigned either to the left or to the right side of ; without loss of generality to the left side of . For two stops  we denote the instance restricted to the stops by . We denote the first stop of  by  and the last stop by . The transitivity property directly yields the next lemma.

###### Lemma 1

Let , and be stops with  be a labeling of , and . Any  intersecting  also intersects .

###### Proof

Recall for the proof that we assume that  satisfies Assumptions 13.

Assume for the sake of contradiction that there is a candidate  such that intersects  but not ; see Fig. 6(c). Since  is a labeling, the labels  and  do not intersect. Hence, neither  and nor and  intersect. Since all three labels are assigned to the same side of , the transitivity property holds, which directly contradicts that  and  do not intersect.∎

Hence, the lemma states that  separates  from the candidates of the stops succeeding . We use this observation as follows. Based on we define a directed acyclic graph ; see Fig. 7(a)–(b). This graph contains a vertex  for each candidate  and the two vertices  and . We call  the source and the target of . Let  denote the candidate that belongs to the vertex . For each pair the graph contains the edge  if and only if the stop of lies directly before the stop of  and, furthermore, and do not intersect. Further, for each vertex  of any candidate of  the graph contains the edge , and for each vertex  of any candidate of  the graph contains the edge . For an edge we define its cost  as follows. For  and  we set . For we set  and for we set .

An - path  in  is a path in  that starts at  and ends at . Its costs are . The - path with minimum costs among all - paths is the shortest - path.

###### Lemma 2

For any - path  in  there is a labeling  of  with  and for any labeling  of there is an - path  in  with .

###### Proof

Recall for the proof that we assume that  satisfies Assumptions 13.

Let be an - path in and let , where denotes the vertices of and the edges of . We show that  is a labeling of  with . Obviously, for each stop  the set  contains exactly one candidate . By construction for each edge the labels  and do not intersect. Hence, by Lemma 1 the label  cannot intersect any label  of any stop that occurs before the stop of . Hence, the set is a labeling. Let  be the labels in  in the order of their stops. It holds

 w(P)=∑\mathclape∈EPwe=w1(ℓ1)+n∑i=2(w1(ℓi)+w2(ℓi−1,ℓi))=w(L) (1)

Now, let  be an arbitrary labeling of . We show that there is an - path  with . Let and be two consecutive stops with  and let  and be the corresponding labels in . Since  and  do not intersect, the corresponding vertices  and of and are adjacent in . Hence, the labels in  induce a path  in . Let  be the labels in  in the order of their stops. Using Equation 1 we obtain .∎

The lemma in particular proves that a shortest - path  in  corresponds with an optimal labeling of . Due to [4, Chapter 24] can be constructed in  time using a dynamic programming approach, which we call MinPath. In particular MinPath considers each edge only once. There are vertices in  and each vertex has at most  incoming edges, which implies that there are edges. Since MinPath considers each edge only once, we compute the edges of  on demand, which saves storage.

###### Theorem 4.1

If is one-sided, SoftMetroLineLabeling can be optimally solved in  time and  space.

#### Labels on Both Sides.

If candidates lie on both sides of the metro line, we solve the problem utilizing the algorithm for the one-sided case.

Consider a labeling  of and let , be two switchovers in  such that  lies before  and no other switchover lies in between both; see Fig. 7(c). Roughly spoken, and  induce a two-sided instance that lies before  and a one-sided instance that lies in between both switchovers and .

###### Lemma 3

Let , , and be stops with ; and  are consecutive. Let  be a labeling of , , , s.t.  is a switchover. Any intersecting  intersects  or .

###### Proof

Recall for the proof that we assume that  satisfies Assumptions 13.

Assume for the sake of contradiction that there is a label  such that  intersects  without intersecting  and . Since  and  intersect each other, due to Assumption 1 both are assigned to the same side of ; w.l.o.g., let  and be assigned to the left hand side of . Further, w.l.o.g., let  be a left candidate and  a right candidate; analogous arguments hold for the opposite case. Since  is a labeling, the labels  and do not intersect. Hence, neither and nor and  intersect. Since , and are assigned to the same side of , the transitivity property must hold. However, this contradicts that  and  intersect.∎

Hence, the lemma yields that for the one-sided instance we can choose any labeling; as long as this labeling does not intersect any label of  or , it composes with , and the labeling of the two-sided instance to one labeling for the instance up to . We use that observation as follows.

Let  and be two switchovers in . Let  and be the stops of  and , and let  and be the stops of  and , respectively; see Fig. 7(c). We assume that , i.e., . Let  be the instance restricted to the stops , where indicates that the stops of belong to that instance, while the stops of do not.

The switchovers  and are compatible if  and are assigned to the same side of , and there is a labeling for  such that it contains , , and and, furthermore, and are the only switchovers in that labeling. Let  be the optimal labeling among those labelings. We denote the labeling  of  by . Utilizing Theorem 4.1, we obtain  in time.

For any labeling of an instance let  be the label of the first stop in  and let  be the label of the last stop in ; is the head and is the tail of . For technical reasons we extend  by the dummy stops , , and such that  for any stop . For and we introduce the dummy switchover  and for and the dummy switchover . We define that  and are compatible to all switchovers in  and that and are compatible, if there is a one-sided labeling for . Conceptually, each dummy switchover consists of two labels that are assigned to both sides of . Further, neither nor  has any influence on the cost of a labeling. Hence, w.l.o.g. we assume that they are contained in any labeling.

Similar to the one-sided case we define a directed acyclic graph . This graph contains a vertex  for each switchover . Let  denote the switchover that belongs to the vertex . In particular let  denote the vertex of  and  denote the vertex of . For each pair the graph contains the edge  if and only if  and are compatible and . The cost  of an edge  in is , where . In the special case that  and share a stop, we set , where .

Let be an - path in and let  be the edges of . For a vertex  of  with we write  instead of . We denote the set by .

###### Lemma 4

a) The graph has an - path if and only if  has a labeling.
b) Let  be a shortest - path in , then is an optimal labeling of .

###### Proof

Recall for the proof that we assume that  satisfies Assumption 13.

By construction of , it directly follows that  has an - path  if and only if has a labeling . We first show that is a labeling of  with . Afterwards we prove that for any labeling  of it holds .

Let  be the edges of with  and . For a vertex  with we write  instead of . We show by induction over  with  that

 Lm:=m⋃i=1Aσi−1,σi

is a labeling of  with . Altogether this implies that is a labeling with .

For  we have . By the construction of the set is a labeling. Since is a dummy switchover we have and . Hence, it holds .

Now, consider the set  for . We first argue that  is a labeling of . By induction the set  is a labeling of . Further, by construction the set is a labeling of the instance . Since  and are compatible, no label of intersects any label of . Then by Lemma 3 no two labels in intersect each other.

We now show that . By induction we have . Since it holds

 w(Lm)=w(Lm−1∪Aσm−1,σm). (2)

We distinguish two cases. First assume that  and do not have any stop in common. Let , then we derive from Equation (2)

 w(Lm)= w(Lm−1∪Aσm−1,σm) = w(Lm−1)+w(Aσm−1,σm)+w3(σm−1,σm)+w2(ℓm−12,hAσm−1,σm) \lx@stackrel(I)= w(Lm−1)+wem\lx@stackrel(II)=m∑i=1wei.

Equality (I) holds due to the definition of and Equality (II) is by induction true. Now assume that  and have a stop in common. Let , then we derive from Equation (2)

 w(Lm)= w(Lm−1∪Aσm−1,σm) = w(Lm−1)+w3(σm−1,σm)+w2(ℓm1,ℓm2)+w1(ℓm2) \lx@stackrel(III)= w(Lm−1)+wem\lx@stackrel(IV)=m∑i=1wei.

Equality (III) holds due to the definition of and Equality (IV) is by induction true. Altogether we obtain that is a labeling with .

Finally we show, that there is no other labeling  with . Assume for the sake of contradiction that there is such a labeling . Let  be the switchovers in , such that for each . We observe that two consecutive switchovers  and  are compatible. Hence, for any  with  there is an edge  in  with  and . Consequently, the edges  form an - path . By the first two claims of this lemma, there is a labeling  of  with . Since  is a shortest - path it holds

 w(L)

We now show that deriving a contradiction. To that end recall that

 LQ=l⋃i=1Aσi−1,σi.

For the set is an optimal labeling of the instance  such that is a labeling and  is the only switchover contained in . Let  and