예제 #1
0
Persistent::Base::Error BayesNet::read(StreamIO &sio, int, int)
{
   bool err = !sio.read( &nextNodeId );
   int numNodes = 0;
   int numSVals;
   if ( !err && sio.read( &numSVals ) )
      for ( int i=0; i<numSVals; i++ )
      {
         float val;
         err = !sio.read(&val);
         statePointer->push_back( new StateValue(val) );
      }

   if ( !err )
      err = !sio.read( &numNodes );

   Error error=Ok;
   for ( int i=0; !err && !error && i<numNodes; i++ )
   {
      Persistent::Base *base = Persistent::Base::load( sio, &error );
      BayesNode *pBN = dynamic_cast<BayesNode *>(base);
      if ( pBN && (error == Ok) )
      {
         addNode( pBN );
         pBN->resolveParents();  
      }
   }
   TopologicalSort();
   return ((!err)? Persistent::Base::Ok : Persistent::Base::ReadError);
}
예제 #2
0
void TGraphCascade::PruneGraph() {
    // iterate over nodes
    int Nodes = NodeNmIdH.Len();
    TIntV NodeIdV;  NodeNmIdH.GetDatV(NodeIdV);
    TStrV NodeNmV;  NodeNmIdH.GetKeyV(NodeNmV);

    for (int NodeN = 0; NodeN < Nodes; NodeN++) {
        int NodeId = NodeIdV[NodeN];
        if (!EnabledNodeIdH.IsKey(NodeId)) {
            // if a node is not enabled:
            // - connect its parents to its children
            TNGraph::TNodeI NI = Graph.GetNI(NodeId);
            for (int ParentN = 0; ParentN < NI.GetInDeg(); ParentN++) {
                for (int ChildN = 0; ChildN < NI.GetOutDeg(); ChildN++) {
                    if (!Graph.IsEdge(NI.GetInNId(ParentN), NI.GetOutNId(ChildN))) {
                        Graph.AddEdge(NI.GetInNId(ParentN), NI.GetOutNId(ChildN));
                    }
                }
            }
            //printf("deleting node %s %d\n", NodeNmV[NodeN].CStr(), NodeId);
            // - delete it (deletes edges)
            Graph.DelNode(NodeId);
        }
    }

    // generate search sequence from sinks to sources
    TopologicalSort(NIdSweep);
    //Print(NIdSweep);
}
예제 #3
0
파일: Algo7-4.c 프로젝트: beike2020/source
 void main()
 {
   ALGraph f;
   printf("请选择有向图\n");
   CreateGraph(&f);
   Display(f);
   TopologicalSort(f);
 }
int main(void)
{    
	MGraph G;  
	GraphAdjList GL; 
	int result;   
	CreateMGraph(&G);
	CreateALGraph(G,&GL);
	result=TopologicalSort(GL);
	printf("result:%d",result);

	return 0;
}
void calculate_topological_sort_result_index(GRAPHIC_TYPE graphic,int* topological_sort_result_index) {
	ElementType *topological_sort_result;
	TopologicalSort(graphic, &topological_sort_result);

	int i = 0;
	for (i = 0; i < graphic -> vertex_count; i++) {
		int vertex_value = topological_sort_result[i];
		AdjacentMatrixGraphicVertex *vertex = GetVertex(graphic, vertex_value);
		topological_sort_result_index[i] = vertex - graphic -> vertex_list;
	}

	free(topological_sort_result);
}
/* 求关键路径,GL为有向网,输出G的各项关键活动 */
void CriticalPath(GraphAdjList GL) 
{    
	EdgeNode *e;    
	int i,gettop,k,j;    
	int ete,lte;  /* 声明活动最早发生时间和最迟发生时间变量 */        
	TopologicalSort(GL);   /* 求拓扑序列,计算数组etv和stack2的值 */ 
	ltv=(int *)malloc(GL->numVertexes*sizeof(int));/* 事件最早发生时间数组 */   
	for(i=0; i<GL->numVertexes; i++)        
		ltv[i]=etv[GL->numVertexes-1];    /* 初始化 */        
	
	printf("etv:\t");   
	for(i=0; i<GL->numVertexes; i++)        
		printf("%d -> ",etv[i]);    
	printf("\n"); 

	while(top2!=0)    /* 出栈是求ltv */    
	{        
		gettop=stack2[top2--];        
		for(e = GL->adjList[gettop].firstedge; e; e = e->next)        /* 求各顶点事件的最迟发生时间ltv值 */        
		{            
			k=e->adjvex;            
			if(ltv[k] - e->weight < ltv[gettop])               
				ltv[gettop] = ltv[k] - e->weight;        
		}   
	}    
	
	printf("ltv:\t");   
	for(i=0; i<GL->numVertexes; i++)        
		printf("%d -> ",ltv[i]);    
	printf("\n"); 

	for(j=0; j<GL->numVertexes; j++)        /* 求ete,lte和关键活动 */        
	{            
		for(e = GL->adjList[j].firstedge; e; e = e->next)            
		{                
			k=e->adjvex;                
			ete = etv[j];        /* 活动最早发生时间 */                
			lte = ltv[k] - e->weight; /* 活动最迟发生时间 */               
			if(ete == lte)    /* 两者相等即在关键路径上 */                    
				printf("<v%d - v%d> length: %d \n",GL->adjList[j].data,GL->adjList[k].data,e->weight);
		}        
	}
}
예제 #7
0
int main() {
  int T;
  scanf("%d", &T);

  for (int t = 0; t < T; ++t) {
    std::vector<std::string> words;
    scanf("%d", &N);

    for (int i = 0; i < N; ++i) {
      char buf[128];
      scanf("%s", buf);
      words.push_back(buf);
    }

    MakeGraph(words);
    std::vector<int> r = TopologicalSort();

    PrintVInt(r);
  }
  //
  return 0;
}
예제 #8
0
/**
 * 算法7.14,G为有向网,输出G的各项关键活动
 * 栈T中保存图G的拓补序列,ve数组中为各顶点的最早开始时间
 */
Status CriticalPath(ALGraph G, SqStack &T, int ve[])
{
	int i, j, k, dut, ee ,el;
	ArcNode *p;
	int vl[MAX_VERTEX_NUM];
	if (ERROR == TopologicalSort(G, T, ve)) {
		return ERROR;
	}
	for (i = 0; i < G.vexnum; i++) {		//初始化顶点的最迟发生时间
		vl[i] = ve[G.vexnum-1];
	}
	while (!StackEmpty(T)) {			//按拓补逆序求各顶点的最晚开始时间vl值
		Pop(T, j);
		for (p = G.vertices[j].firstarc; p != NULL; p = p->nextarc) {
			k = p->adjvex;
			dut = atoi(p->info);		//活动时间
			if (vl[k] - dut < vl[j]) {
				vl[j] = vl[k] - dut;
			}
		}
	}

	printf("关键活动:");
	for (j = 0; j < G.vexnum; j++) {	//求活动的最早开始时间和最晚开始时间,并求出关键活动
		for (p = G.vertices[j].firstarc; p != NULL; p = p->nextarc) {
			k = p->adjvex;
			dut = atoi(p->info);
			ee = ve[j];		//活动的最早开始时间
			el = vl[k]-dut;		//活动的最晚开始时间
			if (ee == el) {		//关键活动
				printf("%c%c ", G.vertices[j].data, G.vertices[k].data);
			}
		}
	}
	printf("\n");
}
예제 #9
0
bool CriticalGraph::ComputeCriticalPath(
        const CriticalNode* from,
        const CriticalNode* to,
        const std::unordered_set<uint32_t>& tids,
        CriticalPath* path) const
{
    assert(from != nullptr);
    assert(to != nullptr);
    assert(path != nullptr);

    // Topological sort.
    std::vector<const CriticalNode*> topological;
    if (!TopologicalSort(from, to, &topological)) {
        tberror() << "Destination node wasn't found in topological sort." << tbendl();
        return false;
    }

    // Compute maximum distance from destination for each node.
    std::unordered_map<const CriticalNode*, NodeDistance> distances;

    for (auto it = topological.rbegin(); it != topological.rend(); ++it)
    {
        // If the node is the destination, the distance is zero.
        if (*it == to)
        {
            distances[*it] = NodeDistance(0, kInvalidCriticalEdgeId);
            continue;
        }

        // Compute the 2 possible distances.
        size_t horizontal_distance = kHugeDistance;
        CriticalEdgeId horizontal_edge_id = (*it)->edge(kCriticalEdgeOutHorizontal);

        if (horizontal_edge_id != kInvalidCriticalEdgeId)
        {
            const auto& edge = GetEdge(horizontal_edge_id);
            auto distance_look = distances.find(edge.to());
            if (distance_look != distances.end()) {
                horizontal_distance = distance_look->second.distance;
                if (horizontal_distance != kHugeDistance)
                {
                    if (tids.find((*it)->tid()) == tids.end())
                    {
                        // Reduce the cost of edges that are on other threads.
                        horizontal_distance = std::min(horizontal_distance, static_cast<size_t>(1));
                    }
                    horizontal_distance += edge.Cost();
                }
            }
        }

        size_t vertical_distance = kHugeDistance;
        CriticalEdgeId vertical_edge_id = (*it)->edge(kCriticalEdgeOutVertical);

        if (vertical_edge_id != kInvalidCriticalEdgeId)
        {
            const auto& edge = GetEdge(vertical_edge_id);
            auto distance_look = distances.find(edge.to());
            if (distance_look != distances.end()) {
                vertical_distance = distance_look->second.distance;
                if (vertical_distance != kHugeDistance)
                    vertical_distance += edge.Cost();
            }
        }

        // Keep the maximum distance which is not invalid.
        if (horizontal_distance != kHugeDistance &&
            (vertical_distance == kHugeDistance || horizontal_distance >= vertical_distance))
        {
            distances[*it] = NodeDistance(horizontal_distance, horizontal_edge_id);
        }
        else if (vertical_distance != kHugeDistance &&
                 (horizontal_distance == kHugeDistance || vertical_distance >= horizontal_distance))
        {
            distances[*it] = NodeDistance(vertical_distance, vertical_edge_id);
        }
    }

    // Retrieve the critical path.
    const CriticalNode* cur = from;
    while (cur != to)
    {
        const NodeDistance& distance = distances[cur];
        const CriticalEdge& edge = GetEdge(distance.edge);

        if (edge.type() != CriticalEdgeType::kVertical)
        {
            path->Push(CriticalPathSegment(
                edge.from()->ts(), edge.from()->tid(), edge.type()));
        }

        cur = GetEdge(distance.edge).to();
    }

    // Restrict the critical path to the autorized threads.
    path->RestrictToThreads(tids);

    return true;
}