• Articles
  • Topological Sort of Strings- part I
Published by
Jul 27, 2014 (last update: Nov 7, 2014)

Topological Sort of Strings- part I

Score: 3.7/5 (136 votes)
*****
Well, I wonder how a compiler finds out dependencies in a header file( one headers needs another and another needs the other and blah blah blah). So let's say -

 We have 4 headers named "A", "B", "C" and "D"
--> "A" depends on "B" and "C"
--> "B" depends on "D"
--> "C" doesn't depend on anyone
--> "D" depends on "C"

Here's our problem- How all these headers will be sorted so that before including a header all it's dependencies will be met. And here comes topological sorting. We should sort them like this -

C -- D -- B -- A

So now all dependencies will be met !

Here's how I did that -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class StringTopoSort
{
	public :
		StringTopoSort(vector<string> [],int);
		~StringTopoSort();
		
		vector<string> string_topo_sort();
	private :
		void visit(int index);
		int len;
		vector<string> * str_lists;
		vector<string> unsorted;
		vector<string> sorted;
		vector<int>	* digit_eq;
		vector<int> digit_sorted;
		vector<bool> is_visited;
};


StringTopoSort :: StringTopoSort(vector<string> * _str_lists,int _len)
{
	str_lists = _str_lists;
	len = _len;
	digit_eq = new vector<int>[len];
	for(int i =0; i<len;i++)
	{
		is_visited.push_back(false);
		unsorted.push_back(str_lists[i].at(0));
	}
	for(int i =0; i<len;i++)
	{
		for(vector<string>::iterator it = str_lists[i].begin(); it<str_lists[i].end();++it)
		{
			vector<string> :: iterator index = find(unsorted.begin(),unsorted.end(),*it);
			if(index != unsorted.end() )
				digit_eq[i].push_back(index-unsorted.begin());
		}
	}
}

StringTopoSort :: ~StringTopoSort() {}

vector<string> StringTopoSort :: string_topo_sort()
{
	for(int i =0;i<len; i++)
	{
		if(is_visited[i] == false)
			visit(i);
	}
	
	for(int i = 0; i<digit_sorted.size();i++)
		sorted.push_back(unsorted[digit_sorted[i]]);
	
	return sorted;
}

void StringTopoSort :: visit(int index)
{
	is_visited[index] = true;
	
	for(vector<int>::iterator i = digit_eq[index].begin(); i<digit_eq[index].end(); i++)
	{
		if(!is_visited[*i])
			visit(*i);
	}
	digit_sorted.push_back(index);
}


int main(int argc, char ** argv)
{
	vector<string> headers[] = {{"A","B","C"},{"B","D"},{"C"},{"D","C"}};
	StringTopoSort sorter(headers,4);
	vector<string> sorted = sorter.string_topo_sort();
	for(int i =0; i<sorted.size(); i++) 
		cout << sorted[i] << " -- ";
	cout << endl;
	return 0;
}


And here we go -


C -- D -- B -- A --

The StringTopoSort doesn't need c++11, but the declaration of headers here needs
this. So you have to compile it by -

g++ main.cpp -std=c++11