concept segment in category algorithms

appears as: segment, segment, segments, segments
Algorithms and Data Structures in Action

This is an excerpt from Manning's book Algorithms and Data Structures in Action.

Some of the most interesting results in this area focus on complete and complete bipartite graphs: Guy’s and Zarankiewicz’ conjectures postulate a formula for the crossing number of these graphs, but they haven’t been proved so far.

Figure 15.12 A naïve embedding for K6: there are 15 crossing pair of edges with this layout.

Guy’s conjecture hypothesizes that the minimum crossing number of the generic complete graph with n vertices is given by:

This, however, requires a bit too much fiddling with edge cases (f.i. parallel segments), so I prefer a different, more elegant, method I recently discovered.

The gist of it is shown in figure 15.18: it doesn’t involve bounding boxes anymore, rather we want to find the intersection point of the lines passing through the segments, and then check that it lies within both segments. There are, actually, three possible cases:

  • The intersection of the two lines, point P, lie outside of both segments;
  • P lies inside one of the two segments, but not both;
  • Listing 16.2 Method validateEmbedding
    function validateEmbedding(graph, embedding)                #1
      n ← |graph.vertices|
      for i in {0..n-2} do                            #2
        for j in {i+1..n-1} do                           
          if vertexIntersectsVertex(embedding[i], embedding[j]) then        #3
            return false
      for edge in graph.edges do                        #4
        for vertex in graph.vertices do                    #5
            if vertex != edge.source and vertex != edge.destination and
                edgeIntersectsVertex(
                  embedding[indexOf(edge.source)],   
                  embedding[indexOf(edge.destination)],
                  embedding[indexOf(vertex)]) then                #6
              return false
      return true