Crossings Minimization  1.0
crossing_utilities.c
Go to the documentation of this file.
1 /**
2  * @file crossings.c
3  * @brief Implementation of functions that are used to count and update
4  * crossings locally.
5  *
6  * @author Matt Stallmann
7  * @date 2009/05/15
8  * $Id: crossing_utilities.c 56 2014-03-13 21:12:01Z mfms $
9  */
10 
11 #include"graph.h"
12 #include"defs.h"
13 #include"crossing_utilities.h"
14 #include"sorting.h"
15 
16 #include<stdio.h>
17 #include<stdlib.h>
18 #include<assert.h>
19 
20 #ifdef DEBUG
21 /**
22  * Prints an edge (for debugging)
23  */
24 static void print_edge( Edgeptr edge )
25 {
26  printf("[%d, %d]", edge->up_node->position, edge->down_node->position );
27 }
28 
29 /**
30  * Prints the current sequence of edges between the layers - used for
31  * debugging
32  */
33 static void print_edge_array( Edgeptr * edge_array, int number_of_edges )
34 {
35  int i = 0;
36  for( ; i < number_of_edges; i++ )
37  {
38  print_edge( edge_array[i] );
39  printf("\n");
40  }
41 }
42 #endif
43 
44 /**
45  * Updates crossings for edges and their endpoints when two edges form an
46  * inversion.
47  *
48  * @param diff indicates whether to increment the number of crossings for
49  * nodes and edges (+1) or decrement them (-1)
50  */
51 static void update_crossings( Edgeptr edge_one, Edgeptr edge_two, int diff )
52 {
53  edge_one->crossings += diff;
54  edge_two->crossings += diff;
55  Nodeptr up_node_one = edge_one->up_node;
56  Nodeptr up_node_two = edge_two->up_node;
57  Nodeptr down_node_one = edge_one->down_node;
58  Nodeptr down_node_two = edge_two->down_node;
59  up_node_one->down_crossings += diff;
60  up_node_two->down_crossings += diff;
61  down_node_one->up_crossings += diff;
62  down_node_two->up_crossings += diff;
63 }
64 
66  int starting_index,
67  int diff )
68 {
69  int number_of_crossings = 0;
70  int index = starting_index - 1;
71  Edgeptr edge_to_insert = edge_array[starting_index];
72  while( index >= 0
73  && edge_array[index]->down_node->position
74  > edge_to_insert->down_node->position )
75  {
76  number_of_crossings++;
77  update_crossings( edge_array[index], edge_to_insert, diff );
78  edge_array[index + 1] = edge_array[index];
79  index--;
80  }
81  edge_array[index + 1] = edge_to_insert;
82  return number_of_crossings;
83 }
84 
85 int count_inversions_down( Edgeptr * edge_array, int number_of_edges, int diff )
86 {
87 #ifdef DEBUG
88  printf("-> count_inversions_down\n");
89  printf( " edge array for upper layer %d:\n",
90  edge_array[0]->up_node->layer );
91  print_edge_array( edge_array, number_of_edges );
92 #endif
93  int number_of_inversions = 0;
94  int i = 1;
95  for( ; i < number_of_edges; i++ )
96  {
97  number_of_inversions
98  += insert_and_count_inversions_down( edge_array, i, diff );
99  }
100 #ifdef DEBUG
101  printf("<- count_inversions_down, number = %d\n", number_of_inversions);
102  printf( " edge array for upper layer %d:\n",
103  edge_array[0]->up_node->layer );
104  print_edge_array( edge_array, number_of_edges );
105 #endif
106  return number_of_inversions;
107 }
108 
110  int starting_index, int diff )
111 {
112  int number_of_crossings = 0;
113  int index = starting_index - 1;
114  Edgeptr edge_to_insert = edge_array[starting_index];
115  while( index >= 0
116  && edge_array[index]->up_node->position
117  > edge_to_insert->up_node->position )
118  {
119  number_of_crossings++;
120  update_crossings( edge_array[index], edge_to_insert, diff );
121  edge_array[index + 1] = edge_array[index];
122  index--;
123  }
124  edge_array[index + 1] = edge_to_insert;
125  return number_of_crossings;
126 }
127 
128 int count_inversions_up( Edgeptr * edge_array, int number_of_edges, int diff )
129 {
130 #ifdef DEBUG
131  printf("-> count_inversions_up\n");
132  print_edge_array( edge_array, number_of_edges );
133 #endif
134  int number_of_inversions = 0;
135  int i = 1;
136  for( ; i < number_of_edges; i++ )
137  {
138  number_of_inversions
139  += insert_and_count_inversions_up( edge_array, i, diff );
140  }
141 #ifdef DEBUG
142  printf("<- count_inversions_up, number = %d\n", number_of_inversions);
143  print_edge_array( edge_array, number_of_edges );
144 #endif
145  return number_of_inversions;
146 }
147 
148 void add_edges_to_array( Edgeptr * edge_array, Edgeptr * edges_to_add,
149  int num_edges, int start_pos )
150 {
151  int edges_added = 0;
152  for( ; edges_added < num_edges; edges_added++ )
153  {
154  edge_array[ start_pos + edges_added ] = edges_to_add[ edges_added ];
155  }
156 }
157 
158 /* [Last modified: 2014 03 10 at 16:39:24 GMT] */
Nodeptr up_node
Definition: graph.h:98
int down_crossings
Definition: graph.h:87
Definition of functions that are used to count and update crossings locally. Used both by crossings...
Definitions common to all edge crossing heuristic source files.
int position
Definition: graph.h:73
Interface for functions that perform various sorts. All sorts are stable.
int insert_and_count_inversions_down(Edgeptr *edge_array, int starting_index, int diff)
void add_edges_to_array(Edgeptr *edge_array, Edgeptr *edges_to_add, int num_edges, int start_pos)
int insert_and_count_inversions_up(Edgeptr *edge_array, int starting_index, int diff)
int up_crossings
Definition: graph.h:86
int crossings
Definition: graph.h:100
static void update_crossings(Edgeptr edge_one, Edgeptr edge_two, int diff)
Definition of data structures and access functions for a layered graph.
int count_inversions_down(Edgeptr *edge_array, int number_of_edges, int diff)
int number_of_edges
Definition: graph_io.c:29
Nodeptr down_node
Definition: graph.h:99
int count_inversions_up(Edgeptr *edge_array, int number_of_edges, int diff)