Ejemplo n.º 1
0
SlnTable MarkingBoolVertex(const BoolVertexEdge_list &main_table, const std::vector< std::set< BoolVertex > > &boolvertexcond) {

	Graph< std::set< BoolVertex >, AreStrongComponentsVertexConnected,  std::vector< std::set< BoolVertex > >::const_iterator, 
	       BoolVertexEdge_list::const_iterator > g2(boolvertexcond.begin(), boolvertexcond.end(), main_table.begin(), main_table.end());
	cVector temp = g2.topological_sort();
	std::deque< vSet > top_sort(temp.begin(), temp.end());

	SlnTable solution;
	cSet is_marked;

	while (!top_sort.empty()) {
	   if (is_marked.find(*top_sort.rbegin()) == is_marked.end()) {
		   solution[*top_sort.rbegin()] = 0;
		   is_marked.insert(*top_sort.rbegin());
	   }
	   if (is_marked.find(*(top_sort.rend() - 1)) == is_marked.end()) {
		   solution[*(top_sort.rend() - 1)] = 1;
		   is_marked.insert(*(top_sort.rend() - 1));
	   }
	   std::for_each(top_sort.rbegin(), top_sort.rend(), [&] (const vSet &v) {
		   if (StrongComponentsAreOpposite(std::make_pair(v, *top_sort.rbegin()))) {
				solution[v] = 1;
				is_marked.insert(v);
		   }
		   if (StrongComponentsAreOpposite(std::make_pair(v, *(top_sort.rend() - 1)))) {
				solution[v] = 0;
				is_marked.insert(v);
		   }
	   });
	   top_sort.pop_back();
	   top_sort.pop_front();
	}

	return solution;
}
Ejemplo n.º 2
0
bool Doc2Vec::obj_knn_objs(const char * search, real * src,
  bool search_is_word, bool target_is_word,
  knn_item_t * knns, int k)
{
  long long a = -1, b, c, target_size;
  real * search_vectors, * target, * target_vectors;
  Vocabulary * search_vocab, * target_vocab;
  search_vocab = search_is_word ? m_word_vocab : m_doc_vocab;
  search_vectors = search_is_word ? m_nn->m_syn0norm : m_nn->m_dsyn0norm;
  target_vectors = target_is_word ? m_nn->m_syn0norm : m_nn->m_dsyn0norm;
  target_size = target_is_word ? m_nn->m_vocab_size : m_nn->m_corpus_size;
  target_vocab = target_is_word ? m_word_vocab : m_doc_vocab;
  if(!src) {
    a = search_vocab->searchVocab(search);
    if(a < 0) return false;
    src = &(search_vectors[a * m_nn->m_dim]);
  }
  for(b = 0, c = 0; b < target_size; b++)
  {
    if(search_is_word == target_is_word && a == b) continue;
    target = &(target_vectors[b * m_nn->m_dim]);
    if(c < k){
      knns[c].similarity = similarity(src, target);
      knns[c].idx = b;
      c++;
      if(c == k) top_init(knns, k);
    }
    else top_collect(knns, k, b, similarity(src, target));
  }
  top_sort(knns, k);
  for(b = 0; b < k; b++) strcpy(knns[b].word, target_vocab->m_vocab[knns[b].idx].word);
  return true;
}
Ejemplo n.º 3
0
int main()
{
	FILE *file;
	/* graph[i][j] == 1 iff there is a sequence "ij" in the input */
	int graph[10][10];
	int sort[10];
	/* Nodes that occur in input. We want the shortest sequence so we assume
	 * that no nodes are in the graph to start with, but we do need to keep
	 * track of nodes with no outgoing edges */
	int nodes[10];
	char curr[3];
	int i;

	if ((file = fopen("./keylog-short.txt", "r")) == NULL)
	{
		printf("fopen failed\n");
		return -1;
	}

	/* Initialize data structures */
	memset(graph, 0, 10*10*sizeof(int));
	memset(nodes, 0, 10*sizeof(int));

	while (fscanf(file, "%s", curr) != EOF)
	{
		graph[curr[0] - '0'][curr[1] - '0'] = 1;
		graph[curr[1] - '0'][curr[2] - '0'] = 1;

		nodes[curr[0] - '0'] = 1;
		nodes[curr[1] - '0'] = 1;
		nodes[curr[2] - '0'] = 1;
	}

	top_sort(graph, sort);

	for (i = 0; i < 10 && sort[i] > 0; i++)
	{
		printf("%d", sort[i]);
		// Unset this so we can print the remaining ones in the next
		// step
		nodes[sort[i]] = 0;
	}
	/* Add any numbers which have no successors */
	for (i = 0; i < 10; i++)
	{
		if (nodes[i])
		{
			printf("%d", i);
		}
	}

	printf("\n");

	fclose(file);
	
	return 0;
}
/*
 * Returns the longest path in the given graph. The given graph must be a DAG.
 * The reverse of the graph must also be passed to this function
 */
ll_node* longest_path_dag(graph* g,graph* flipped,int* len_dest) {
   vertex* ord = top_sort(g,flipped);

   int weights[g->numV];
   int parents[g->numV];

   for(int i=g->numV-1;i>=0;i--) {
      weights[i]=0;
      parents[i]=-1;
   }

   for(int i=0;i<g->numV;i++)
      DBG(("%i %i\t",parents[i],weights[i]));
   DBG(("\n"));
   for(int i=0;i<g->numV;i++) {
      vertex v = ord[i];
      for(int j = v.numE-1;j>=0;j--) {
         arc d = v.edges[j];
         if(weights[d.dest] <= weights[v.label] + d.len) {
            weights[d.dest] = weights[v.label] + d.len;
            parents[d.dest] = v.label;
         }
      }
      for(int i=0;i<g->numV;i++)
         DBG(("%i %i\t",parents[i],weights[i]));
      DBG(("\n"));
   }

   int max = 0, maxI = -1;
   for(int i=g->numV-1;i>=0;i--)
      if(weights[i] > max) {
         max = weights[i];
         maxI = i;
      }

   *len_dest = max;
   ll_node* path = NULL;
   int cur = maxI;
   while(cur != -1) {
      DBG(("%i\n",cur));
      ll_node* new_path = malloc(sizeof(struct ll_node));
      new_path->data = (void*)(long)cur;
      new_path->next = path;
      path = new_path;

      cur = parents[cur];
   }

   free(ord);
   return path;
}
Ejemplo n.º 5
0
int glp_top_sort(glp_graph *G, int v_num)
{     glp_vertex *v;
      int i, cnt, *num;
      if (v_num >= 0 && v_num > G->v_size - (int)sizeof(int))
         xerror("glp_top_sort: v_num = %d; invalid offset\n", v_num);
      if (G->nv == 0)
      {  cnt = 0;
         goto done;
      }
      num = xcalloc(1+G->nv, sizeof(int));
      cnt = top_sort(G, num);
      if (v_num >= 0)
      {  for (i = 1; i <= G->nv; i++)
         {  v = G->v[i];
            memcpy((char *)v->data + v_num, &num[i], sizeof(int));
         }
      }
      xfree(num);
done: return cnt;
}
Ejemplo n.º 6
0
static void
assignTrackNo (Dt_t* chans)
{
    Dt_t* lp;
    Dtlink_t* l1;
    Dtlink_t* l2;
    channel* cp;
    int k;

    for (l1 = dtflatten (chans); l1; l1 = dtlink(chans,l1)) {
	lp = ((chanItem*)l1)->chans;
	for (l2 = dtflatten (lp); l2; l2 = dtlink(lp,l2)) {
	    cp = (channel*)l2;
	    if (cp->cnt) {
#ifdef DEBUG
    if (odb_flags & ODB_CHANG) dumpChanG (cp, ((chanItem*)l1)->v);
#endif
		top_sort (cp->G);
		for (k=0;k<cp->cnt;k++)
		    cp->seg_list[k]->track_no = cp->G->vertices[k].topsort_order+1;
	    }
   	}
    }
}
void test_top_sort(Graph *g) {
    int s;
    int i, j;
    int len = g->vertexNum;
    int *result = (int *)malloc(len * sizeof(int));
    if(result == NULL) {
        printf("malloc failed\n");
        return;
    }
    memset(result, 0, len * sizeof(int));
    printf("Input a suggest first node: ");
    scanf("%d", &s);
    top_sort(g, s, result);
    for(i=0;i<len;i++) {
        for(j=0;j<len;j++) {
            if(result[j] == i)
                printf("%d", j);
        }
        if(i < len -1)
            printf("->");
    }
    printf("\n");
    free(result);
}
Ejemplo n.º 8
0
int main(int argc, char** argv)
{
  int m;
  int i;
  char s[4];
  while(scanf("%d%d", &n, &m) == 2)
  {
    t = n;
    memset(G, 0, sizeof(G));
    for(i = 0; i < m; i++)
    {
      scanf("%s", s);
      G[s[0]-'a'][s[2]-'a'] = 1;
    }
    if(top_sort())
    {
      for(i = 0; i < n; i++)
        printf("%c ", 'a'+ans[i]);
      printf("\n");
    }
    else printf("-1\n");
  }
  return 0;
}