Variable Sequence Code of topological sorting

Source: Internet
Author: User

Variable Sequence Code of topological sorting
/*
Name:
Copyright:
Author:
Date: 17-11-14
Description: variable sequence of topological sorting
Assume that there are n variables (1 <= n <= 26, the variable name is represented by a single lowercase letter), and m binary groups (u, v ), the u variable is smaller than v. So what should all variables look like from small to large?
For example, there are four variables a, B, c, d. Input
The input is a string of data, which contains N + N characters, indicating N relational expressions (1 <= N <= 100000). For example, the sequence "abcbdc" indicates Output
A string that stores a sequence of variables that meet the requirements. For example, the string "adcb" indicates
*/
# Include
# Include


# Define true 1
# Define false 0
# Define MAXM 26 // maximum number of variables (vertices)
# Define MAXN 100000 // maximum number of relational expressions


Typedef char VertexType; // The vertex type is customized by the user.
Typedef int EdgeType; // The weight type on the edge is customized by the user.


Typedef struct EdgeNode {// edge table Node
Int adjvex; // The subscripts corresponding to the vertex in the adjacent vertex field.
// EdgeType weight; // weight value, which is not required for non-network graphs
Struct EdgeNode * next; // link field, pointing to the next adjacent point
} EdgeNode;


Typedef struct VertexNode {// vertex table Node
VertexType data; // vertex domain, storing vertex Information
Int in; // stores the number of vertex inputs.
EdgeNode * firstEdge; // edge header pointer
} VertexNode;


Typedef struct Edge {// Number of Edge Sets
Int u, v; // arc tail and arc Header
Int next; // point to the next edge at the end of the same arc
// EdgeType weight; // weight value, which is not required for non-network graphs
} EdgeLib;


Int book [MAXM] = {0}; // mark whether a letter appears


Int IsTopoSeq (char * data, char * topo); // determines whether the topo string is a topological sequence based on the data in the Link List.
Int CreateGraph (char * data, VertexNode * GL); // create a graph
Void PrintGraph (VertexNode * GL); // output Graph
Int TopoLogicalSort_DFS (char * topo, VertexNode * GL, int n); // obtains the topological sequence. If a ring exists, false is returned.
Int TopoLogicalSort_BFS (char * topo, VertexNode * GL, int n); // obtains the topological sequence. If a ring exists, false is returned.
Int creategraph2 (char * data, int In [], int first [], EdgeLib edge []); // create an image
Void PrintGraph_2 (int first [], EdgeLib edge []); // output Graph
Int TopoLogicalSort (char * topo, EdgeLib edge [], int In [], int first [], int n); // topological sorting to obtain the topological sequence, if a ring exists, false is returned. The queue is used to store the topological sequence.


Int main ()
{
Int I, n;
VertexNode GL [MAXM];
Char topo [MAXM + 1];
Char data [MAXN + 1];
Int In [MAXM], first [MAXM]; // store vertex Information
EdgeLib edge [MAXN]; // stores edge information


Gets (data );
N = creategraph2 (data, In, first, edge); // create a graph.
PrintGraph_2 (first, edge); // output Graph

If (TopoLogicalSort (topo, edge, In, first, n) // use topological sorting to construct a topological Sequence
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");

If (IsTopoSeq (data, topo) // determines whether the topo string is a topological Sequence Based on the relational list data.
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");

Gets (data );
N = CreateGraph (data, GL); // create a graph
PrintGraph (GL); // output Graph
If (IsTopoSeq (data, topo) // determines whether the topo string is a topological Sequence Based on the relational list data.
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");

If (TopoLogicalSort_BFS (topo, GL, n) // use topological sorting to construct a topological Sequence
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");

Gets (data );
N = CreateGraph (data, GL); // create a graph
PrintGraph (GL); // output Graph

If (TopoLogicalSort_DFS (topo, GL, n) // use topological sorting to construct a topological Sequence
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");

If (IsTopoSeq (data, topo) // determines whether the topo string is a topological Sequence Based on the relational list data.
Puts (topo );
Else
Puts ("a sequence meeting the condition does not exist ");




Return 0;
}
/*
Function Name: CreateGraph
Function: Read vertices and edges in the adjacent table indicating the graph.
Input variable: char * data: string that stores N relational expressions
VertexNode * GL: vertex Table Array
Output variable: the vertex table array of the graph.
Return Value: int: Number of vertices
*/
Int CreateGraph (char * data, VertexNode * GL)
{
Int I, u, v;
Int count = 0; // Number of vertex records
EdgeNode * e;

For (I = 0; I {
GL [I]. data = I + 'a ';
GL [I]. in = 0;
GL [I]. firstEdge = NULL;
Book [I] = 0;
}

For (I = 0; data [I]! = '\ 0'; I + = 2) // read two variables each time.
{
U = data [I]-'A'; // convert letters to numbers, 'A' corresponds to 0, 'B' corresponds to 1, and so on
V = data [I + 1]-'A ';
Book [u] = book [v] = 1;

E = (EdgeNode *) malloc (sizeof (EdgeNode); // insert edge table nodes using the header Insertion Method
If (! E)
{
Puts ("Error ");
Exit (1 );
}
E-> adjvex = v;
E-> next = GL [u]. firstEdge;
GL [u]. firstEdge = e;

GL [v]. in ++;
}

For (I = 0; I {
If (book [I]! = 0)
Count ++;
}

Return count;
}


Void PrintGraph (VertexNode * GL) // output Graph
{
Int u, v;
EdgeNode * e;

For (u = 0; u {
Printf ("G [% d] = % c:", u, GL [u]. data );
For (e = GL [u]. firstEdge; e! = NULL; e = e-> next) // reduce the inbound degree of the adjacent point of u by 1, and add the vertex with the inbound degree of 0 to the stack.
{
V = e-> adjvex;
Printf ("<% c, % c>,", GL [u]. data, GL [v]. data );
}
Printf ("\ n ");
}
Printf ("\ n ");
}


/*
Function Name: TopoLogicalSort_DFS
Function: Performs topological sorting and obtains the topological sequence using deep priority search.
Input variable: char * topo: string used to store the topological Sequence
VertexNode * GL: vertex Table Array
Int n: Number of vertices
Output variable: string used to store the topological Sequence
Return Value: int: returns true if the topology is sorted successfully. If a ring exists, false is returned.
*/
Int TopoLogicalSort_DFS (char * topo, VertexNode * GL, int n)
{
Int I, u, v, top;
Int count = 0; // used to count the number of output vertices
EdgeNode * e;
Int Stack [MAXM];

For (top = I = 0; I {
If (book [I]! = 0 & GL [I]. in = 0)
{
Stack [top ++] = I;
}
}

While (top> 0) // obtain the topological sequence with deep Priority Search
{
U = Stack [-- top];
Topo [count ++] = u + 'a ';

For (e = GL [u]. firstEdge; e! = NULL; e = e-> next) // reduce the inbound degree of the adjacent point of u by 1, and add the vertex with the inbound degree of 0 to the stack.
{
V = e-> adjvex;
If (-- GL [v]. in = 0)
Stack [top ++] = v;
}
}
Topo [count] = '\ 0 ';

Return (count = n); // If count is smaller than the number of vertices, a ring exists.
}


/*
Function Name: TopoLogicalSort_BFS
Function: Performs topological sorting and returns the topological sequence using the breadth-first search.
Input variable: char * topo: string used to store the topological Sequence
VertexNode * GL: vertex Table Array
Int n: Number of vertices
Output variable: string used to store the topological Sequence
Return Value: int: returns true if the topology is sorted successfully. If a ring exists, false is returned.
*/
Int TopoLogicalSort_BFS (char * topo, VertexNode * GL, int n)
{
Int I, u, v, front, rear;
EdgeNode * e;

Front = rear = 0;
For (I = 0; I {
If (book [I]! = 0 & GL [I]. in = 0)
{
Topo [rear ++] = I + 'a ';
}
}

While (front <rear) // obtain the topological sequence using the breadth-first search
{
U = topo [front ++]-'A ';

For (e = GL [u]. firstEdge; e! = NULL; e = e-> next) // reduce the inbound degree of the adjacent point of u by 1, and add the vertex with the inbound degree of 0 to the stack.
{
V = e-> adjvex;
If (-- GL [v]. in = 0)
Topo [rear ++] = v + 'a ';
}
}
Topo [rear] = '\ 0 ';

Return (rear = n); // If count is smaller than the number of vertices, a ring exists.
}


/*
Function Name: creategraph2
Function: Read vertex and edge information into the edge table set that represents the graph.
Input variable: char * data: string that stores N relational expressions
Int In []: stores vertex inbound Information
Int first []: point to the first edge ending with this vertex as the Arc
EdgeLib edge []: edge table set that stores edge information
Output variable: Number of edge table sets of a graph
Return Value: int: Number of vertices
*/
Int creategraph2 (char * data, int In [], int first [], EdgeLib edge []) // create a graph
{
Int I, j;
Int count = 0; // Number of vertex records

For (I = 0; I {
First [I] =-1;
Book [I] = 0;
In [I] = 0;
}

For (j = I = 0; data [I]! = '\ 0'; I + = 2, j ++) // read two variables each time.
{
Edge [j]. u = data [I]-'A'; // convert a letter to a number. 'A' corresponds to 0, 'B' corresponds to 1, and so on.
Edge [j]. v = data [I + 1]-'A ';
Book [edge [j]. u] = book [edge [j]. v] = 1;

Edge [j]. next = first [edge [j]. u];
First [edge [j]. u] = j;
In [edge [j]. v] ++;
}

For (I = 0; I {
If (book [I]! = 0)
Count ++;
}

Return count;
}


Void PrintGraph_2 (int first [], EdgeLib edge []) // output Graph
{
Int I, j;

For (I = 0; I {
Printf ("G [% d] = % c:", I, I + 'A ');
J = first [I]; // point to the first edge of I
While (j! =-1)
{
Printf ("<% c, % c>,", edge [j]. u + 'A', edge [j]. v + 'A ');
J = edge [j]. next; // point to the next edge
}
Printf ("\ n ");
}
Printf ("\ n ");
}
/*
Function Name: TopoLogicalSort
Function: Performs topological sorting and returns the topological sequence using the breadth-first search.
Input variable: char * topo: string used to store the topological Sequence
EdgeLib edge []: edge table set that stores edge information
Int In []: stores vertex inbound Information
Int first []: point to the first edge ending with this vertex as the Arc
Int n: Number of vertices
Output variable: string used to store the topological Sequence
Return Value: int: returns true if the topology is sorted successfully. If a ring exists, false is returned.
*/
Int TopoLogicalSort (char * topo, EdgeLib edge [], int In [], int first [], int n)
{
Int I, u, front, rear;

Front = rear = 0;
For (I = 0; I {
If (book [I]! = 0 & In [I] = 0)
{
Topo [rear ++] = I + 'a ';
}
}

While (front <rear) // obtain the topological sequence using the breadth-first search
{
U = topo [front ++]-'A ';
For (I = first [u]; I! =-1; I = edge [I]. next)
{
If (-- In [edge [I]. v] = 0)
Topo [rear ++] = edge [I]. v + 'a ';
}
}
Topo [rear] = '\ 0 ';

Return (rear = n); // If count is smaller than the number of vertices, a ring exists.
}


Int IsTopoSeq (char * data, char * topo) // determines whether the topo string is a topological sequence based on the data in the Link List.
{
Int pos [MAXM] = {0 };
Int I;

For (I = 0; topo [I]! = '\ 0'; I ++) // read the variable subscript
Pos [topo [I]-'a'] = I;

For (I = 0; data [I]! = '\ 0'; I + = 2) // read two variables each time.
{
If (pos [data [I]-'a']> pos [data [I + 1]-'a'])
Return false;
}

Return true;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.