Skip to content
PBQP.cpp 33.8 KiB
Newer Older
//===---------------- PBQP.cpp --------- PBQP Solver ------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Developed by:                   Bernhard Scholz
//                             The University of Sydney
//                         http://www.it.usyd.edu.au/~scholz
//===----------------------------------------------------------------------===//


#include <limits>
#include <cassert>
Andrew Lenharth's avatar
Andrew Lenharth committed
#include <cstring>
#include "llvm/Config/alloca.h"

namespace llvm {

/**************************************************************************
 * Data Structures 
 **************************************************************************/

/* edge of PBQP graph */
typedef struct adjnode {
  struct adjnode *prev,      /* doubly chained list */ 
                 *succ, 
                 *reverse;   /* reverse edge */
  int adj;                   /* adj. node */
  PBQPMatrix *costs;         /* cost matrix of edge */

  bool tc_valid;              /* flag whether following fields are valid */
  int *tc_safe_regs;          /* safe registers */
  int tc_impact;              /* impact */ 
} adjnode;

/* bucket node */
typedef struct bucketnode {
  struct bucketnode *prev;   /* doubly chained list */
  struct bucketnode *succ;   
  int u;                     /* node */
} bucketnode;

/* data structure of partitioned boolean quadratic problem */
struct pbqp {
  int num_nodes;             /* number of nodes */
  int max_deg;               /* maximal degree of a node */
  bool solved;               /* flag that indicates whether PBQP has been solved yet */
  bool optimal;              /* flag that indicates whether PBQP is optimal */
  PBQPNum min;
  bool changed;              /* flag whether graph has changed in simplification */

                             /* node fields */
  PBQPVector **node_costs;   /* cost vectors of nodes */
  int *node_deg;             /* node degree of nodes */
  int *solution;             /* solution for node */
  adjnode **adj_list;        /* adj. list */
  bucketnode **bucket_ptr;   /* bucket pointer of a node */

                             /* node stack */
  int *stack;                /* stack of nodes */
  int stack_ptr;             /* stack pointer */

                             /* bucket fields */
  bucketnode **bucket_list;  /* bucket list */

  int num_r0;                /* counters for number statistics */
  int num_ri;
  int num_rii;
  int num_rn; 
  int num_rn_special;      
};

bool isInf(PBQPNum n) { return n == std::numeric_limits<PBQPNum>::infinity(); } 

/*****************************************************************************
 * allocation/de-allocation of pbqp problem 
 ****************************************************************************/

/* allocate new partitioned boolean quadratic program problem */
pbqp *alloc_pbqp(int num_nodes)
{
  pbqp *this_;
  int u;
  
  assert(num_nodes > 0);
  
  /* allocate memory for pbqp data structure */   
  this_ = (pbqp *)malloc(sizeof(pbqp));

  /* Initialize pbqp fields */
  this_->num_nodes = num_nodes;
  this_->solved = false;
  this_->optimal = true;
  this_->min = 0.0;
  this_->max_deg = 0;
  this_->changed = false;
  this_->num_r0 = 0;
  this_->num_ri = 0;
  this_->num_rii = 0;
  this_->num_rn = 0;
  this_->num_rn_special = 0;
  
  /* initialize/allocate stack fields of pbqp */ 
  this_->stack = (int *) malloc(sizeof(int)*num_nodes);
  this_->stack_ptr = 0;
  
  /* initialize/allocate node fields of pbqp */
  this_->adj_list = (adjnode **) malloc(sizeof(adjnode *)*num_nodes);
  this_->node_deg = (int *) malloc(sizeof(int)*num_nodes);
  this_->solution = (int *) malloc(sizeof(int)*num_nodes);
  this_->bucket_ptr = (bucketnode **) malloc(sizeof(bucketnode **)*num_nodes);
  this_->node_costs = (PBQPVector**) malloc(sizeof(PBQPVector*) * num_nodes);
  for(u=0;u<num_nodes;u++) {
    this_->solution[u]=-1;
    this_->adj_list[u]=NULL;
    this_->node_deg[u]=0;
    this_->bucket_ptr[u]=NULL;
    this_->node_costs[u]=NULL;
  }
  
  /* initialize bucket list */
  this_->bucket_list = NULL;
  
  return this_;
}

/* free pbqp problem */
void free_pbqp(pbqp *this_)
{
  int u;
  int deg;
  adjnode *adj_ptr,*adj_next;
  bucketnode *bucket,*bucket_next;
  
  assert(this_ != NULL);
  
  /* free node cost fields */
  for(u=0;u < this_->num_nodes;u++) {
    delete this_->node_costs[u];
  }
  free(this_->node_costs);
  
  /* free bucket list */
  for(deg=0;deg<=this_->max_deg;deg++) {
    for(bucket=this_->bucket_list[deg];bucket!=NULL;bucket=bucket_next) {
      this_->bucket_ptr[bucket->u] = NULL;
      bucket_next = bucket-> succ;
      free(bucket);
    }
  }
  free(this_->bucket_list);
  
  /* free adj. list */
  assert(this_->adj_list != NULL);
  for(u=0;u < this_->num_nodes; u++) {
    for(adj_ptr = this_->adj_list[u]; adj_ptr != NULL; adj_ptr = adj_next) {
      adj_next = adj_ptr -> succ;
      if (u < adj_ptr->adj) {
        assert(adj_ptr != NULL);
        delete adj_ptr->costs;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
      }
      if (adj_ptr -> tc_safe_regs != NULL) {
           free(adj_ptr -> tc_safe_regs);
      }
      free(adj_ptr);
    }
  }
  free(this_->adj_list);
  
  /* free other node fields */
  free(this_->node_deg);
  free(this_->solution);
  free(this_->bucket_ptr);

  /* free stack */
  free(this_->stack);

  /* free pbqp data structure itself */
  free(this_);
}


/****************************************************************************
 * adj. node routines 
 ****************************************************************************/

/* find data structure of adj. node of a given node */
static
adjnode *find_adjnode(pbqp *this_,int u,int v)
{
  adjnode *adj_ptr;
  
  assert (this_ != NULL);
  assert (u >= 0 && u < this_->num_nodes);
  assert (v >= 0 && v < this_->num_nodes);
  assert(this_->adj_list != NULL);

  for(adj_ptr = this_ -> adj_list[u];adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
    if (adj_ptr->adj == v) {
      return adj_ptr;
    }
  }
  return NULL;
}

/* allocate a new data structure for adj. node */
static
adjnode *alloc_adjnode(pbqp *this_,int u, PBQPMatrix *costs)
{
  adjnode *p;

  assert(this_ != NULL);
  assert(costs != NULL);
  assert(u >= 0 && u < this_->num_nodes);

  p = (adjnode *)malloc(sizeof(adjnode));
  assert(p != NULL);
  
  p->adj = u;
  p->costs = costs;  

  p->tc_valid= false;
  p->tc_safe_regs = NULL;
  p->tc_impact = 0;

  return p;
}

/* insert adjacence node to adj. list */
static
void insert_adjnode(pbqp *this_, int u, adjnode *adj_ptr)
{

  assert(this_ != NULL);
  assert(adj_ptr != NULL);
  assert(u >= 0 && u < this_->num_nodes);

  /* if adjacency list of node is not empty -> update
     first node of the list */
  if (this_ -> adj_list[u] != NULL) {
    assert(this_->adj_list[u]->prev == NULL);
    this_->adj_list[u] -> prev = adj_ptr;
  }

  /* update doubly chained list pointers of pointers */
  adj_ptr -> succ = this_->adj_list[u];
  adj_ptr -> prev = NULL;

  /* update adjacency list pointer of node u */
  this_->adj_list[u] = adj_ptr;
}

/* remove entry in an adj. list */
static
void remove_adjnode(pbqp *this_, int u, adjnode *adj_ptr)
{
  assert(this_!= NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  assert(this_->adj_list != NULL);
  assert(adj_ptr != NULL);
  
  if (adj_ptr -> prev == NULL) {
    this_->adj_list[u] = adj_ptr -> succ;
  } else {
    adj_ptr -> prev -> succ = adj_ptr -> succ;
  } 

  if (adj_ptr -> succ != NULL) {
    adj_ptr -> succ -> prev = adj_ptr -> prev;
  }

  if(adj_ptr->reverse != NULL) {
    adjnode *rev = adj_ptr->reverse;
    rev->reverse = NULL;
  }

  if (adj_ptr -> tc_safe_regs != NULL) {
     free(adj_ptr -> tc_safe_regs);
  }

  free(adj_ptr);
}

/*****************************************************************************
 * node functions 
 ****************************************************************************/

/* get degree of a node */
static
int get_deg(pbqp *this_,int u)
{
  adjnode *adj_ptr;
  int deg = 0;
  
  assert(this_ != NULL);
  assert(u >= 0 && u < this_->num_nodes);
  assert(this_->adj_list != NULL);

  for(adj_ptr = this_ -> adj_list[u];adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
    deg ++;
  }
  return deg;
}

/* reinsert node */
static
void reinsert_node(pbqp *this_,int u)
{
  adjnode *adj_u,
          *adj_v;

  assert(this_!= NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  assert(this_->adj_list != NULL);

  for(adj_u = this_ -> adj_list[u]; adj_u != NULL; adj_u = adj_u -> succ) {
    int v = adj_u -> adj;
    adj_v = alloc_adjnode(this_,u,adj_u->costs);
    insert_adjnode(this_,v,adj_v);
  }
}

/* remove node */
static
void remove_node(pbqp *this_,int u)
{
  adjnode *adj_ptr;

  assert(this_!= NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  assert(this_->adj_list != NULL);

  for(adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
    remove_adjnode(this_,adj_ptr->adj,adj_ptr -> reverse);
  }
}

/*****************************************************************************
 * edge functions
 ****************************************************************************/

/* insert edge to graph */
/* (does not check whether edge exists in graph */
static
void insert_edge(pbqp *this_, int u, int v, PBQPMatrix *costs)
{
  adjnode *adj_u,
          *adj_v;
  
  /* create adjanceny entry for u */
  adj_u = alloc_adjnode(this_,v,costs);
  insert_adjnode(this_,u,adj_u);


  /* create adjanceny entry for v */
  adj_v = alloc_adjnode(this_,u,costs);
  insert_adjnode(this_,v,adj_v);
  
  /* create link for reverse edge */
  adj_u -> reverse = adj_v;
  adj_v -> reverse = adj_u;
}

/* delete edge */
static
void delete_edge(pbqp *this_,int u,int v)
{
  adjnode *adj_ptr;
  adjnode *rev;
  
  assert(this_ != NULL);
  assert( u >= 0 && u < this_->num_nodes);
  assert( v >= 0 && v < this_->num_nodes);

  adj_ptr=find_adjnode(this_,u,v);
  assert(adj_ptr != NULL);
  assert(adj_ptr->reverse != NULL);

  delete adj_ptr -> costs;
 
  rev = adj_ptr->reverse; 
  remove_adjnode(this_,u,adj_ptr);
  remove_adjnode(this_,v,rev);
} 

/*****************************************************************************
 * cost functions 
 ****************************************************************************/

/* Note: Since cost(u,v) = transpose(cost(v,u)), it would be necessary to store 
   two matrices for both edges (u,v) and (v,u). However, we only store the 
   matrix for the case u < v. For the other case we transpose the stored matrix
   if required. 
*/

/* add costs to cost vector of a node */
void add_pbqp_nodecosts(pbqp *this_,int u, PBQPVector *costs)
{
  assert(this_ != NULL);
  assert(costs != NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  
  if (!this_->node_costs[u]) {
    this_->node_costs[u] = new PBQPVector(*costs);
  } else {
    *this_->node_costs[u] += *costs;
  }
}

/* get cost matrix ptr */
static
PBQPMatrix *get_costmatrix_ptr(pbqp *this_, int u, int v)
{
  adjnode *adj_ptr;
  PBQPMatrix *m = NULL;

  assert (this_ != NULL);
  assert (u >= 0 && u < this_->num_nodes);
  assert (v >= 0 && v < this_->num_nodes); 

  adj_ptr = find_adjnode(this_,u,v);

  if (adj_ptr != NULL) {
    m = adj_ptr -> costs;
  } 

  return m;
}

/* get cost matrix ptr */
/* Note: only the pointer is returned for 
   cost(u,v), if u < v.
*/ 
static
PBQPMatrix *pbqp_get_costmatrix(pbqp *this_, int u, int v)
{
  adjnode *adj_ptr = find_adjnode(this_,u,v);
  
  if (adj_ptr != NULL) {
    if ( u < v) {
      return new PBQPMatrix(*adj_ptr->costs);
    } else {
      return new PBQPMatrix(adj_ptr->costs->transpose());
    }
  } else {
    return NULL;
  }  
}

/* add costs to cost matrix of an edge */
void add_pbqp_edgecosts(pbqp *this_,int u,int v, PBQPMatrix *costs)
{
  PBQPMatrix *adj_costs;

  assert(this_!= NULL);
  assert(costs != NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  assert(v >= 0 && v <= this_->num_nodes);
  
  /* does the edge u-v exists ? */
  if (u == v) {
    PBQPVector *diag = new PBQPVector(costs->diagonalize());
    add_pbqp_nodecosts(this_,v,diag);
    delete diag;
  } else if ((adj_costs = get_costmatrix_ptr(this_,u,v))!=NULL) {
    if ( u < v) {
      *adj_costs += *costs;
    } else {
      *adj_costs += costs->transpose();
    }
  } else {
    adj_costs = new PBQPMatrix((u < v) ? *costs : costs->transpose());
    insert_edge(this_,u,v,adj_costs);
  } 
}

/* remove bucket from bucket list */
static
void pbqp_remove_bucket(pbqp *this_, bucketnode *bucket)
{
  int u = bucket->u;
  
  assert(this_ != NULL);
  assert(u >= 0 && u < this_->num_nodes);
  assert(this_->bucket_list != NULL);
  assert(this_->bucket_ptr[u] != NULL);
  
  /* update predecessor node in bucket list 
     (if no preceeding bucket exists, then
     the bucket_list pointer needs to be 
     updated.)
  */    
  if (bucket->prev != NULL) {
    bucket->prev-> succ = bucket->succ; 
  } else {
    this_->bucket_list[this_->node_deg[u]] = bucket -> succ;
  }
  
  /* update successor node in bucket list */ 
  if (bucket->succ != NULL) { 
    bucket->succ-> prev = bucket->prev;
  }
}

/**********************************************************************************
 * pop functions
 **********************************************************************************/

/* pop node of given degree */
static
int pop_node(pbqp *this_,int deg)
{
  bucketnode *bucket;
  int u;

  assert(this_ != NULL);
  assert(deg >= 0 && deg <= this_->max_deg);
  assert(this_->bucket_list != NULL);
   
  /* get first bucket of bucket list */
  bucket = this_->bucket_list[deg];
  assert(bucket != NULL);

  /* remove bucket */
  pbqp_remove_bucket(this_,bucket);
  u = bucket->u;
  free(bucket);
  return u;
}

/**********************************************************************************
 * reorder functions
 **********************************************************************************/

/* add bucket to bucketlist */
static
void add_to_bucketlist(pbqp *this_,bucketnode *bucket, int deg)
{
  bucketnode *old_head;
  
  assert(bucket != NULL);
  assert(this_ != NULL);
  assert(deg >= 0 && deg <= this_->max_deg);
  assert(this_->bucket_list != NULL);

  /* store node degree (for re-ordering purposes)*/
  this_->node_deg[bucket->u] = deg;
  
  /* put bucket to front of doubly chained list */
  old_head = this_->bucket_list[deg];
  bucket -> prev = NULL;
  bucket -> succ = old_head;
  this_ -> bucket_list[deg] = bucket;
  if (bucket -> succ != NULL ) {
    assert ( old_head -> prev == NULL);
    old_head -> prev = bucket;
  }
}


/* reorder node in bucket list according to 
   current node degree */
static
void reorder_node(pbqp *this_, int u)
{
  int deg; 
  
  assert(this_ != NULL);
  assert(u>= 0 && u < this_->num_nodes);
  assert(this_->bucket_list != NULL);
  assert(this_->bucket_ptr[u] != NULL);

  /* get current node degree */
  deg = get_deg(this_,u);
  
  /* remove bucket from old bucket list only
     if degree of node has changed. */
  if (deg != this_->node_deg[u]) {
    pbqp_remove_bucket(this_,this_->bucket_ptr[u]);
    add_to_bucketlist(this_,this_->bucket_ptr[u],deg);
  } 
}

/* reorder adj. nodes of a node */
static
void reorder_adjnodes(pbqp *this_,int u)
{
  adjnode *adj_ptr;
  
  assert(this_!= NULL);
  assert(u >= 0 && u <= this_->num_nodes);
  assert(this_->adj_list != NULL);

  for(adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = adj_ptr -> succ) {
    reorder_node(this_,adj_ptr->adj);
  }
}

/**********************************************************************************
 * creation functions
 **********************************************************************************/

/* create new bucket entry */
/* consistency of the bucket list is not checked! */
static
void create_bucket(pbqp *this_,int u,int deg)
{
  bucketnode *bucket;
  
  assert(this_ != NULL);
  assert(u >= 0 && u < this_->num_nodes);
  assert(this_->bucket_list != NULL);
  
  bucket = (bucketnode *)malloc(sizeof(bucketnode));
  assert(bucket != NULL);

  bucket -> u = u;
  this_->bucket_ptr[u] = bucket;

  add_to_bucketlist(this_,bucket,deg);
}

/* create bucket list */
static
void create_bucketlist(pbqp *this_)
{
  int u;
  int max_deg;
  int deg;

  assert(this_ != NULL);
  assert(this_->bucket_list == NULL);

  /* determine max. degree of the nodes */
  max_deg = 2;  /* at least of degree two! */
  for(u=0;u<this_->num_nodes;u++) {
    deg = this_->node_deg[u] = get_deg(this_,u);
    if (deg > max_deg) {
      max_deg = deg;
    }
  }
  this_->max_deg = max_deg;
  
  /* allocate bucket list */
  this_ -> bucket_list = (bucketnode **)malloc(sizeof(bucketnode *)*(max_deg + 1));
  memset(this_->bucket_list,0,sizeof(bucketnode *)*(max_deg + 1));
  assert(this_->bucket_list != NULL);
  
  /* insert nodes to the list */
  for(u=0;u<this_->num_nodes;u++) {
    create_bucket(this_,u,this_->node_deg[u]);  
  }
}

/*****************************************************************************
 * PBQP simplification for trivial nodes
 ****************************************************************************/

/* remove trivial node with cost vector length of one */
static
void disconnect_trivialnode(pbqp *this_,int u)
{
  int v;
  adjnode *adj_ptr, 
          *next;
  PBQPMatrix *c_uv;
  PBQPVector *c_v;
  
  assert(this_ != NULL);
  assert(this_->node_costs != NULL);
  assert(u >= 0 && u < this_ -> num_nodes);
  assert(this_->node_costs[u]->getLength() == 1);
  
  /* add edge costs to node costs of adj. nodes */
  for(adj_ptr = this_->adj_list[u]; adj_ptr != NULL; adj_ptr = next){
    next = adj_ptr -> succ;
    v = adj_ptr -> adj;
    assert(v >= 0 && v < this_ -> num_nodes);
    
    /* convert matrix to cost vector offset for adj. node */
    c_uv = pbqp_get_costmatrix(this_,u,v);
    c_v = new PBQPVector(c_uv->getRowAsVector(0));
    *this_->node_costs[v] += *c_v;
    
    /* delete edge & free vec/mat */
    delete c_v;
    delete c_uv;
    delete_edge(this_,u,v);
  }   
}

/* find all trivial nodes and disconnect them */
static   
void eliminate_trivial_nodes(pbqp *this_)
{
   int u;
   
   assert(this_ != NULL);
   assert(this_ -> node_costs != NULL);
   
   for(u=0;u < this_ -> num_nodes; u++) {
     if (this_->node_costs[u]->getLength() == 1) {
       disconnect_trivialnode(this_,u); 
     }
   }
}

/*****************************************************************************
 * Normal form for PBQP 
 ****************************************************************************/

/* simplify a cost matrix. If the matrix
   is independent, then simplify_matrix
   returns true - otherwise false. In
   vectors u and v the offset values of
   the decomposition are stored. 
*/

static
bool normalize_matrix(PBQPMatrix *m, PBQPVector *u, PBQPVector *v)
{
  assert( m != NULL);
  assert( u != NULL);
  assert( v != NULL);
  assert( u->getLength() > 0);
  assert( v->getLength() > 0);
  
  assert(m->getRows() == u->getLength());
  assert(m->getCols() == v->getLength());

  /* determine u vector */
  for(unsigned r = 0; r < m->getRows(); ++r) {
    PBQPNum min = m->getRowMin(r);
    (*u)[r] += min;
    if (!isInf(min)) {
      m->subFromRow(r, min);
    } else {
      m->setRow(r, 0);
    }
  }
  
  /* determine v vector */
  for(unsigned c = 0; c < m->getCols(); ++c) {
    PBQPNum min = m->getColMin(c);
    (*v)[c] += min;
    if (!isInf(min)) {
      m->subFromCol(c, min);
    } else {
      m->setCol(c, 0);
    }
  }
  
  /* determine whether matrix is 
     independent or not. 
    */
  return m->isZero();
}

/* simplify single edge */
static
void simplify_edge(pbqp *this_,int u,int v)
{
  PBQPMatrix *costs;
  bool is_zero; 
  
  assert (this_ != NULL);
  assert (u >= 0 && u <this_->num_nodes);
  assert (v >= 0 && v <this_->num_nodes);
  assert (u != v);

  /* swap u and v  if u > v in order to avoid un-necessary
     tranpositions of the cost matrix */
  
  if (u > v) {
    int swap = u;
    u = v;
    v = swap;
  }
  
  /* get cost matrix and simplify it */  
  costs = get_costmatrix_ptr(this_,u,v);
  is_zero=normalize_matrix(costs,this_->node_costs[u],this_->node_costs[v]);

  /* delete edge */
  if(is_zero){
    delete_edge(this_,u,v);
    this_->changed = true;
  }
}

/* normalize cost matrices and remove 
   edges in PBQP if they ary independent, 
   i.e. can be decomposed into two 
   cost vectors.
*/
static
void eliminate_independent_edges(pbqp *this_)
{
  int u,v;
  adjnode *adj_ptr,*next;
  
  assert(this_ != NULL);
  assert(this_ -> adj_list != NULL);

  this_->changed = false;
  for(u=0;u < this_->num_nodes;u++) {
    for (adj_ptr = this_ -> adj_list[u]; adj_ptr != NULL; adj_ptr = next) {
      next = adj_ptr -> succ;
      v = adj_ptr -> adj;
      assert(v >= 0 && v < this_->num_nodes);
      if (u < v) {
        simplify_edge(this_,u,v);
      } 
    }
  }
}


/*****************************************************************************
 * PBQP reduction rules 
 ****************************************************************************/

/* RI reduction
   This reduction rule is applied for nodes 
   of degree one. */

static
void apply_RI(pbqp *this_,int x)
{
  int y;
  unsigned xlen,
           ylen;
  PBQPMatrix *c_yx;
  PBQPVector *c_x, *delta;
  
  assert(this_ != NULL);
  assert(x >= 0 && x < this_->num_nodes);
  assert(this_ -> adj_list[x] != NULL);
  assert(this_ -> adj_list[x] -> succ == NULL);

  /* get adjacence matrix */
  y = this_ -> adj_list[x] -> adj;
  assert(y >= 0 && y < this_->num_nodes);
  
  /* determine length of cost vectors for node x and y */
  xlen = this_ -> node_costs[x]->getLength();
  ylen = this_ -> node_costs[y]->getLength();

  /* get cost vector c_x and matrix c_yx */
  c_x = this_ -> node_costs[x];
  c_yx = pbqp_get_costmatrix(this_,y,x); 
  assert (c_yx != NULL);

  
  /* allocate delta vector */
  delta = new PBQPVector(ylen);

  /* compute delta vector */
  for(unsigned i = 0; i < ylen; ++i) {
    PBQPNum min =  (*c_yx)[i][0] + (*c_x)[0];
    for(unsigned j = 1; j < xlen; ++j) {
      PBQPNum c =  (*c_yx)[i][j] + (*c_x)[j];
      if ( c < min )  
         min = c;
    }
    (*delta)[i] = min; 
  } 

  /* add delta vector */
  *this_ -> node_costs[y] += *delta;

  /* delete node x */
  remove_node(this_,x);

  /* reorder adj. nodes of node x */
  reorder_adjnodes(this_,x);

  /* push node x on stack */
  assert(this_ -> stack_ptr < this_ -> num_nodes);
  this_->stack[this_ -> stack_ptr++] = x;

  /* free vec/mat */
  delete c_yx;
  delete delta;

  /* increment counter for number statistic */
  this_->num_ri++;
}

/* RII reduction
   This reduction rule is applied for nodes 
   of degree two. */

static
void apply_RII(pbqp *this_,int x)
{
  int y,z; 
  unsigned xlen,ylen,zlen;
  adjnode *adj_yz;

  PBQPMatrix *c_yx, *c_zx;
  PBQPVector *cx;
  PBQPMatrix *delta;
 
  assert(this_ != NULL);
  assert(x >= 0 && x < this_->num_nodes);
  assert(this_ -> adj_list[x] != NULL);
  assert(this_ -> adj_list[x] -> succ != NULL);
  assert(this_ -> adj_list[x] -> succ -> succ == NULL);

  /* get adjacence matrix */
  y = this_ -> adj_list[x] -> adj;
  z = this_ -> adj_list[x] -> succ -> adj;
  assert(y >= 0 && y < this_->num_nodes);
  assert(z >= 0 && z < this_->num_nodes);
  
  /* determine length of cost vectors for node x and y */
  xlen = this_ -> node_costs[x]->getLength();
  ylen = this_ -> node_costs[y]->getLength();
  zlen = this_ -> node_costs[z]->getLength();

  /* get cost vector c_x and matrix c_yx */
  cx = this_ -> node_costs[x];
  c_yx = pbqp_get_costmatrix(this_,y,x); 
  c_zx = pbqp_get_costmatrix(this_,z,x); 
  assert(c_yx != NULL);
  assert(c_zx != NULL);

  /* Colour Heuristic */
  if ( (adj_yz = find_adjnode(this_,y,z)) != NULL) {
    adj_yz->tc_valid = false;
    adj_yz->reverse->tc_valid = false; 
  }

  /* allocate delta matrix */
  delta = new PBQPMatrix(ylen, zlen);

  /* compute delta matrix */
  for(unsigned i=0;i<ylen;i++) {
    for(unsigned j=0;j<zlen;j++) {
      PBQPNum min = (*c_yx)[i][0] + (*c_zx)[j][0] + (*cx)[0];
      for(unsigned k=1;k<xlen;k++) {
        PBQPNum c = (*c_yx)[i][k] + (*c_zx)[j][k] + (*cx)[k];
        if ( c < min ) {
          min = c;
        }
      }
      (*delta)[i][j] = min;
    }
  }

  /* add delta matrix */
  add_pbqp_edgecosts(this_,y,z,delta);

  /* delete node x */
  remove_node(this_,x);

  /* simplify cost matrix c_yz */
  simplify_edge(this_,y,z);

  /* reorder adj. nodes */
  reorder_adjnodes(this_,x);

  /* push node x on stack */
  assert(this_ -> stack_ptr < this_ -> num_nodes);
  this_->stack[this_ -> stack_ptr++] = x;

  /* free vec/mat */
  delete c_yx;
  delete c_zx;
  delete delta;

  /* increment counter for number statistic */
  this_->num_rii++;

}

/* RN reduction */
static
void apply_RN(pbqp *this_,int x)
{
  unsigned xlen;

  assert(this_ != NULL);
  assert(x >= 0 && x < this_->num_nodes);
  assert(this_ -> node_costs[x] != NULL);

  xlen = this_ -> node_costs[x] -> getLength();

  /* after application of RN rule no optimality
     can be guaranteed! */
  this_ -> optimal = false;
  
  /* push node x on stack */