# Include "iostream. H"
# Include "fstream. H"
# Include "sqstack. H"
# Include "stdlib. H"
# Deprecision Max 100000
# Define max_vertex_num 20
Typedef Enum {DG, dn, udg, UDN} graphkind;
Typedef char vertextype;
Typedef struct {
Vertextype vexs [max_vertex_num];
Int arcs [max_vertex_num] [max_vertex_num];
Int vexnum, arcnum;
Graphkind kind;
} Mgraph;
Int locatevex (mgraph g, vertextype U)
{
For (INT I = 0; I <G. vexnum & G. vexs [I]! = U; I ++ );
If (I = G. vexnum) return false;
Else return I;
}
Status creategraph (mgraph & G)
{
Int N;
Cout <"0. Directed Graph 1. Directed Network," <Endl;
Cout <"2. undirected graph 3. undirected network" <Endl;
Cout <"Enter the type of the graph you want to create:" <Endl;
Cin> N;
Ifstream istrm;
Switch (N)
{
Case 0: G. Kind = DG; istrm. Open ("dgraph.txt"); break;
Case 1: G. Kind = DN; istrm. Open ("dnet.txt"); break;
Case 2: G. Kind = udg; istrm. Open ("udgraph.txt"); break;
Case 3: G. Kind = UDN; istrm. Open ("udnet.txt"); break;
}
Istrm> G. vexnum> G. arcnum;
Int I, J, K, W;
Vertextype V1, V2;
For (I = 0; I <G. vexnum; I ++) istrm> G. vexs [I];
If (N % 2)
{
For (I = 0; I <G. arcnum; I ++)
For (j = 0; j <G. vexnum; j ++)
G. arcs [I] [J] = max;
For (k = 0; k <G. arcnum; k ++)
{
Istrm> V1> V2> W;
I = locatevex (G, V1 );
J = locatevex (G, V2 );
G. arcs [I] [J] = W;
If (n/2) g. arcs [J] [I] = W;
}
}
Else
{
For (I = 0; I <G. arcnum; I ++)
For (j = 0; j <G. vexnum; j ++)
G. arcs [I] [J] = 0;
For (k = 0; k <G. arcnum; k ++)
{
Istrm> V1> V2;
I = locatevex (G, V1 );
J = locatevex (G, V2 );
G. arcs [I] [J] = 1;
If (n/2) g. arcs [J] [I] = 1;
}
}
Return OK;
}
Void output (mgraph g)
{
Cout <"this figure is ";
Switch (G. Kind)
{
Case DG: cout <"Directed Graph" <Endl; break;
Case DN: cout <"" <Endl; break;
Case udg: cout <"undirected graph" <Endl; break;
Case UDN: cout <"undirected network" <Endl; break;
}
Cout <"the number of vertices is" <G. vexnum <Endl;
Cout <"the number of edges is" <G. arcnum <Endl;
Int I, J;
If (G. kind/2)
{
For (I = 0; I <G. vexnum; I ++)
For (j = 0; j <I; j ++)
{
If (G. arcs [I] [J]! = 0 & G. arcs [I] [J]! = Max)
{
Cout <G. vexs [I] <"" <G. vexs [J];
If (G. Kind % 2) cout <"" <G. arcs [I] [J];
Cout <Endl;
}
}
}
Else
{
For (I = 0; I <G. vexnum; I ++)
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [I] [J]! = 0 & G. arcs [I] [J]! = Max)
{
Cout <G. vexs [I] <"" <G. vexs [J];
If (G. Kind % 2) cout <"" <G. arcs [I] [J];
Cout <Endl;
}
}
}
}
Void dfstraverse (mgraph g)
{
Int visited [max_vertex_num], I;
For (I = 0; I <G. vexnum; I ++)
Visited [I] = 0;
For (I = 0; I <G. vexnum; I ++)
{
If (! Visited [I]) continue;
Visited [I] = 1;
Cout <G. vexs [I] <"";
}
}
Void minitree_prim (mgraph g, vertextype U)
{
Struct {
Vertextype adjvex;
Int lowcost;
} Closedge [max_vertex_num];
Int K = locatevex (G, U );
Int I, j, min;
For (j = 0; j <G. vexnum; j ++)
If (J! = K) {closedge [J]. adjvex = u; closedge [J]. lowcost = G. arcs [k] [J];}
Closedge [K]. lowcost = 0;
Cout <"Minimum Spanning Tree is" <Endl;
For (I = 1; I <G. vexnum; I ++)
{Min = max;
For (j = 0; j <G. vexnum; j ++)
{If (closedge [J]. lowcost)
If (closedge [J]. lowcost <min) {k = J; min = closedge [J]. lowcost ;}
}
Cout <closedge [K]. adjvex <"" <G. vexs [k] <Endl;
Closedge [K]. lowcost = 0;
For (j = 0; j <G. vexnum; j ++)
If (G. arcs [k] [J] <closedge [J]. lowcost)
{
Closedge [J]. adjvex = G. vexs [k];
Closedge [J]. lowcost = G. arcs [k] [J];
}
}
}
Status toposort (mgraph g)
{
Cout <"the topological sorting sequence is ";
Int indegree [max_vertex_num] = {0 };
Int I, J;
Vertextype E;
For (I = 0; I <G. vexnum; I ++) // findindegree
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [J] [I]) indegree [I] ++;
}
Sqstack S;
Initstack (s );
For (I = 0; I <G. vexnum; I ++)
{
If (! Indegree [I]) {push (S, G. vexs [I]);}
}
Int COUNT = 0;
// Ofstream ostrm;
// Ostrm. Open ("topoorder.txt ");
While (! Stackempty (s ))
{
Pop (S, e );
Cout <e <""; count ++;
// Ostrm <e <"";
I = locatevex (G, e );
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [I] [J])
{
If (! (-- Indegree [J]) Push (S, G. vexs [J]);
}
}
}
If (count <G. vexnum) return error;
Else Return OK;
Cout <Endl;
}
Status topoorder (mgraph g, sqstack & T, int ve [])
{
Int indegree [max_vertex_num] = {0 };
Int I, J;
Vertextype E;
For (I = 0; I <G. vexnum; I ++) // findindegree
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [J] [I]! = Max) indegree [I] ++;
}
Sqstack S;
Initstack (s );
For (I = 0; I <G. vexnum; I ++)
{
If (! Indegree [I]) {push (S, G. vexs [I]);}
}
Int COUNT = 0;
For (I = 0; I <G. vexnum; I ++)
Ve [I] = 0;
// Ofstream ostrm;
// Ostrm. Open ("topoorder.txt ");
While (! Stackempty (s ))
{
Pop (S, e); push (T, E );
Count ++;
// Ostrm <e <"";
I = locatevex (G, e );
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [I] [J]! = Max)
{
If (! (-- Indegree [J]) Push (S, G. vexs [J]);
If (VE [I] + G. ARCs [I] [J]> ve [J]) {ve [J] = ve [I] + G. ARCs [I] [J];}
}
}
}
If (count <G. vexnum) return error;
Else Return OK;
Cout <Endl;
}
Status criticalpath (mgraph g)
{
Sqstack T;
Initstack (t );
Int I, J, K;
Int EE, El;
Int ve [max_vertex_num], VL [max_vertex_num];
Vertextype E;
If (! Topoorder (G, T, VE) return error;
For (I = 0; I <G. vexnum; I ++) VL [I] = ve [G. vexnum-1];
While (! Stackempty (t ))
{
Pop (T, E); k = locatevex (G, e );
For (j = 0; j <G. vexnum; j ++)
{
If (G. arcs [k] [J]! = Max)
If (VL [J]-G. ARCs [k] [J] <VL [k]) VL [k] = VL [J]-G. ARCs [k] [J];
}
}
// For (I = 0; I <G. vexnum; I ++) cout <"<VL [I]; exit (1 );
For (I = 0; I <G. vexnum; I ++)
For (j = 0; j <G. vexnum; j ++)
If (G. arcs [I] [J]! = Max)
{
EE = ve [I]; El = VL [J]-G. arcs [I] [J];
Char tag = (EE = El )? '*': '+ ';
Cout <G. vexs [I] <"<G. vexs [J] <"<G. ARCs [I] [J] <"<tag <Endl;
}
Return OK;
}
Void shortestpath_dij (mgraph g, vertextype U)
{
Int V0 = locatevex (G, U );
Bool final [max_vertex_num];
Int P [max_vertex_num], d [max_vertex_num];
Int I, j, Min, V;
For (I = 0; I <G. vexnum; I ++)
{
Final [I] = false; d [I] = G. arcs [V0] [I];
If (G. arcs [V0] [I]! = Max) P [I] = V0;
Else P [I] = max;
}
Final [V0] = true;
P [V0] = V0;
D [V0] = 0;
For (I = 1; I <G. vexnum; I ++)
{
Min = max;
For (j = 0; j <G. vexnum; j ++)
If (! Final [J])
If (d [J] <min) {v = J; min = d [J];}
Final [v] = true;
For (j = 0; j <G. vexnum; j ++)
If (! Final [J] & (min + G. arcs [v] [J] <D [J])
{
D [J] = min + G. arcs [v] [J];
P [J] = V;
} // If
} //
For (I = 0; I <G. vexnum; I ++)
{
Cout <G. vexs [I] <"";
J = I;
While (J! = V0)
{
J = P [J];
Cout <G. vexs [J] <"";
}
Cout <D [I] <Endl;
}
} // Dij