void dijkstra(int s, int n, int A[n][n], int D[], int P[], int color[])
{
	int i, current;
	for(i = 0; i < n; i++) {
		P[i] = -1;
		D[i] = 99999;
		color[i] = white;
	}
	D[s - 1] = 0;
	while (1) {
		current = min_temp(n,color,D);
		if (current == -1) {
			return;
		}
		color[current] = black;
		for(i = 0; i < n; i++) {
			if (A[current][i] != 0 && color[i] == white) {
				if (D[current] + A[current][i] < D[i]) {
					P[i] = current + 1;
					D[i] = D[current] + A[current][i];
				}
			}
		}
	}
}
Esempio n. 2
0
void maketree(int r, struct edge tree[MAX])
{	
	int current,i;
	int count = 0;/*number of vertices in the tree*/
	
	/*Initialize all vertices*/
	for(i=0; i<n; i++)
	{
		predecessor[i] = NIL;
		length[i] = infinity;
		status[i] = TEMP;
	}

	/*Make length of root vertex 0*/
	length[r] = 0;

	while(1)
	{
		/*Search for temporary vertex with minimum length
		and  make it current vertex*/
		current = min_temp();
	
		if(current == NIL) 
		{
			if(count == n-1) /*No temporary vertex left*/
				return;
			else /*Temporary vertices left with length infinity*/	 		
			{
				printf("Graph is not connected, No spanning tree possible\n");
				exit(1);
			}
		}
		
		/*Make the current vertex permanent*/
        status[current] = PERM;

		/*Insert the edge ( predecessor[current], current) into the tree,
		except when the current vertex is root*/ 
		if(current != r)
		{
           count++;
		   tree[count].u = predecessor[current];
		   tree[count].v = current;
		}

		for(i=0; i<n; i++)
			if(adj[current][i] > 0 && status[i] == TEMP)
				if(adj[current][i] < length[i])
				{
					predecessor[i] = current;
					length[i] = adj[current][i];
				}
	}
	
}/*End of make_tree( )*/
Esempio n. 3
0
/******************************************************************************
 * Write the various output files and/or plots.                               *
 ******************************************************************************/
void write_diags(int jday, AED_REAL LakeNum)
{
    char ts[20];
    extern AED_REAL Hs, L, T;

    if ( csv_lake_file < 0 ) return;

    //# Output at end of day
    write_time_string(ts, jday, SecsPerDay);

    write_csv_lake("time",            0.0,                       ts,   FALSE);
    write_csv_lake("Volume",          sum_lake_layervol(),       NULL, FALSE);
    write_csv_lake("Vol Snow",        SurfData.HeightSnow * Lake[surfLayer].LayerArea * SurfData.RhoSnow/1000.0,     NULL, FALSE);
    //Magic numbers for ice density are from glm_surface.c
    write_csv_lake("Vol Black Ice",   SurfData.HeightBlackIce * Lake[surfLayer].LayerArea * 917.0/1000.0, NULL, FALSE);
    write_csv_lake("Vol White Ice",   SurfData.HeightWhiteIce * Lake[surfLayer].LayerArea * 890.0/1000.0, NULL, FALSE);
    write_csv_lake("Tot Inflow Vol",  SurfData.dailyInflow,      NULL, FALSE);
    write_csv_lake("Tot Outflow Vol", SurfData.dailyOutflow,     NULL, FALSE);
    write_csv_lake("Overflow Vol",    SurfData.dailyOverflow,    NULL, FALSE);
    write_csv_lake("Evaporation",     SurfData.dailyEvap,        NULL, FALSE);
    write_csv_lake("Rain",            SurfData.dailyRain,        NULL, FALSE);
    write_csv_lake("Snowfall",        SurfData.dailySnow,        NULL, FALSE);
    write_csv_lake("Lake Level",      Lake[surfLayer].Height,    NULL, FALSE);
    write_csv_lake("Surface Area",    Lake[surfLayer].LayerArea, NULL, FALSE);
    write_csv_lake("Black Ice",       SurfData.HeightBlackIce,   NULL, FALSE);
    write_csv_lake("Snow Height",     SurfData.HeightSnow,       NULL, FALSE);
    write_csv_lake("Snow Density",    SurfData.RhoSnow,          NULL, FALSE);
    write_csv_lake("White Ice",       SurfData.HeightWhiteIce,   NULL, FALSE);
    write_csv_lake("Albedo",          SurfData.albedo,           NULL, FALSE);
    write_csv_lake("Max Temp",        max_temp(Lake, NumLayers), NULL, FALSE);
    write_csv_lake("Min Temp",        min_temp(Lake, NumLayers), NULL, FALSE);
    write_csv_lake("Surface Temp",    Lake[surfLayer].Temp,      NULL, FALSE);
    write_csv_lake("Daily Qsw",       SurfData.dailyQsw,         NULL, FALSE);
    write_csv_lake("Daily Qe",        SurfData.dailyQe,          NULL, FALSE);
    write_csv_lake("Daily Qh",        SurfData.dailyQh,          NULL, FALSE);
    write_csv_lake("Daily Qlw",       SurfData.dailyQlw,         NULL, FALSE);
    write_csv_lake("Light",           Lake[surfLayer].Light,     NULL, FALSE);
    write_csv_lake("Benthic Light",   Benthic_Light_pcArea,      NULL, FALSE);

    write_csv_lake("H_s",             Hs,                        NULL, FALSE);
    write_csv_lake("L",               L,                         NULL, FALSE);
    write_csv_lake("T",               T,                         NULL, FALSE);

    write_csv_lake("LakeNumber",      LakeNum,                   NULL, FALSE);
    write_csv_lake("Max dT/dz",    max_dtdz_at(Lake, NumLayers), NULL, FALSE);
    write_csv_lake("coef_wind_drag", coef_wind_drag,             NULL, TRUE);
}
std::vector<Edge> Graph::get_min_incoming_edge() {
	std::vector<int> min_temp(no_of_vertices, 9999.0);
	std::vector<Edge*> edge_temp(no_of_vertices, nullptr); //each vertex would have at most one, except the root which has none
	std::vector<Edge> result;
	for (std::vector<Edge>::iterator it = arr_edge.begin() ; it != arr_edge.end(); it++) {
			if (it->getWeight() < min_temp.at(it->getDestination())) {
				min_temp[it->getDestination()] = it->getWeight();
				edge_temp[it->getDestination()] = &(*it);
			}
	}
	for (int n = 0; n < no_of_vertices; n++) {
		if (edge_temp[n] != nullptr) {
			result.push_back(*(edge_temp[n]));
		}
	}
	return result;
}
void Dijkstra( int s)
{
	int i,current;
	
	/* Make all vertices temporary */
	for(i=0; i<n; i++)
	{
		predecessor[i] =  NIL;   
		pathLength[i] = infinity;
		status[i] = TEMP;
	}
	/* Make pathLength of source vertex equal to 0 */
	pathLength[s] = 0;
		
	while(1)
	{
		/*Search for temporary vertex with minimum pathLength 
		and make it current vertex*/
		current = min_temp( );
	
		if( current == NIL )
			return;
		
		status[current] = PERM;

		for(i=0; i<n; i++)
		{
			/*Checks for adjacent temporary vertices */
			if ( adj[current][i] !=0 && status[i] == TEMP )
				if( pathLength[current] + adj[current][i] < pathLength[i] )
				{	
					predecessor[i] = current;  /*Relabel*/
					pathLength[i] = pathLength[current] + adj[current][i];    
				}
		}
	}
}/*End of Dijkstra( )*/
Esempio n. 6
0
void Dijkstra( int s)
{
	int i,current;
	
	/* Make all vertices temporary */
	for(i=0; i<n; i++)
	{
		predecessor[i] =  NIL;   
		pathLength[i] = infinity;
		status[i] = TEMP;
	}
	/* Make pathLength of source vertex equal to 0 */
	pathLength[s] = 0;
		
	while(1)
	{
		
		/*Search for temporary vertex with minimum pathLength and make it current vertex*/
		displayTable();
		current = min_temp( );
		printf("Current vertex is  %d\n\n", current);
		STOP;

		if( current == NIL )
		{
			printf("Now either there are no temporary vertices or\n");
			printf("all temporary vertices have pathLength infinity\n");
			return;
		}

		status[current] = PERM;

		for(i=0;i<n;i++)
		{
			/*Checks for adjacent temporary vertices */
			if ( adj[current][i] !=0 && status[i] == TEMP )
			{
				printf("Vertex %d is temporary, and is adjacent to current vertex %d\n", i, current);
				if( pathLength[current] + adj[current][i] < pathLength[i] )
				{	
					printf("pathLength(%d) + weight(%d,%d) < pathLength(%d)\n",current,current,i,i);
					printf("%d +  %d < %d\n",pathLength[current],adj[current][i],pathLength[i]);
					printf("Relabel vertex %d\n",i);
					predecessor[i] = current; 
					pathLength[i] = pathLength[current] + adj[current][i];    
					printf("pathLength[%d] = %d,",i,pathLength[current] + adj[current][i]);    
					printf("predecessor[%d] = %d\n\n ",i,current);
				}
				else
				{
					if( pathLength[current] + adj[current][i] == pathLength[i] )
					{
						printf("pathLength(%d) + weight(%d,%d) = pathLength(%d)\n",current,current,i,i);
						printf("%d +  %d = %d\n",pathLength[current],adj[current][i],pathLength[i]);
					}
					else
					{
						printf("pathLength(%d) + weight(%d,%d) > pathLength(%d)\n",current,current,i,i);
						printf("%d +  %d > %d\n",pathLength[current],adj[current][i],pathLength[i]);
					}
					printf("Don't Relabel vertex %d\n\n",i);
				}
				STOP;
			}	
		}
	}
}/*End of Dijkstra( )*/