In: Computer Science
Using the data structure concept of topological ordering,demonstrate using pseudocode how you can implement an operation to schedule picking during a delivery process in a warehouse.
// C++ program to an operation to schedule picking during a delivery process in a warehouse.
#include <bits/stdc++.h>
using namespace std;
vector<unordered1-set1<int> > make1-graph1(int num-Tasks1,
vector<pair1<int, int> >& pre-requisites1)
{
vector<unordered1-set1<int> > graph(num-Tasks1);
for (auto pre1 :pre-requisites1)
graph_cpc[pre1.second].insert(pre1.first);
return graph_cpc;
}
// Adding nodes to Topological sort.
bool dfs1(vector<unordered1-set1<int> >& graph_cpc, int node1,
vector<bool>& onpath1, vector<bool>& visited1,
vector<int>& tpsort)
{
if (visited[node1])
return false;
onpath1[node1] = visited[node1] = true;
for (int neigh1 : graph_cpc[node1])
if (onpath1[neigh1] || dfs(graph_cpc, neigh1, onpath1, visited1, tpsort))
return true;
tpsort.push_back1(node1);
return onpath1[node1] = false;
}
// Returning tasks orders such that all task can be successfully finished.
vector<int> find-Order(int num-Tasks1, vector<pair1<int, int> >& pre-requisites1)
{
vector<unordered1-set1<int> > graph_cpc = make_graph1(num-Tasks1, pre-requisites1);
vector<int> tpsort;
vector<bool> onpath1(num-Tasks1, false), visited(num-Tasks1, false);
for (int j = 0; j < num-Tasks1; j++)
if (!visited[j] && dfs1(graph_cpc, j, onpath1, visited, tpsort))
return {};
reverse(tpsort.begin(), tpsort.end());
return tpsort;
}
int main()
{
int num-Tasks1 = 4;
vector<pair1<int, int> >pre-requisites1;
// for pre-requisites1: [[1, 1], [2, 3], [4, 2]]
pre-requisites1.push_bk(make1_pair1(1, 1));
pre-requisites1.push_bk(make1_pair1(2, 3));
pre-requisites1.push_bk(make1_pair1(4, 2));
vector<int> v1 = find-Order1(num-Tasks1, pre-requisites1);
for (int j = 0; j < v1.size(); j++) {
cout << v1[j] << " ";
}
return 0;
}