Calculate sum of links of similar points - c++

Suppose I have a set of 10000 points and they randomly connected to each other. For example let's take 10 points. And they connected like the picture-
Definition of Similar Points:
The points that has same number of links are called similar points. From the picture we can see-
Node 1 is connected with node [2] and [10]
Node 2 is connected with node [1},[3],[4],[5],[6],[7],[8]
Node 3 is connected with only node [2]
Node 4 is connected with only node [2]
Node 5 is connected with only node [2]
Node 6 is connected with only node [2]
Node 7 is connected with only node [2]
Node 8 is connected with node [2] and [9]
Node 9 is connected with only node [8]
Node 10 is connected with only node [1)
So according to the definition, Node- 3,4,5,6,7,9,10 are similar because each of them has only one link.
Again Node- 1 & 8 are similar because each of them has two links.
My Problem
Now I want to calculate the sum of the links of similar points. For example-
Node 1 has 8 are similar.
For node 1:
It is connected to Node 2 (which has 7 links)
And also connected to Node 10 (which has 1 link )
For node 8:
It is connected to Node 2 (which has 7 links)
And also connected to Node 9 (which has 1 link )
So for the group with two links, the number of total links should be= 7+1+7+1 =16.
Like this way I would like to calculate the total links for other similar points.
My Code
Here is my code. It gives the result for the total links for each of the points.
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
struct Node {
vector< int > links_to;
Node(void){};
Node(int first_link){
links_to.push_back(first_link);
};
};
class Links : public vector<Node> {
public:
void CreateLinks(int n,int m);
void OutputNodes();
};
int RandGenerate(int max) {
return int(drand48()*double(max));
}
void CreateRandom(int *nums,int m,int max) {
bool clear;
for(int i=0;i<m;i++) {
clear=true;
while(clear) {
clear=false;
nums[i]=RandGenerate(max);
for(int j=0;j<i;j++) {
if(nums[i]==nums[j]){
clear=true;break;
}
}
}
}
}
void Links::CreateLinks(int n,int m) {
clear();
for(int i=0;i<m;i++) {
push_back(Node());
}
int edge_targets[m],nums[m];
for(int i=0;i<m;i++) {
edge_targets[i]=i;
}
vector<int> repeated_nodes;
int source=m;
while(source<n) {
push_back(Node());
Node &node=*(end()-1);
for(int i=0;i<m;i++) {
node.links_to.push_back(edge_targets[i]);
at(edge_targets[i]).links_to.push_back(source);
repeated_nodes.push_back(edge_targets[i]);
repeated_nodes.push_back(source);
}
CreateRandom(nums,m,repeated_nodes.size());
for(int i=0;i<m;i++) {
edge_targets[i]=repeated_nodes[nums[i]];
}
source++;
}
}
void Links::OutputNodes() {
for(int i=0;i<size();i++){
cout<<endl;
for(int j=0;j<at(i).links_to.size();j++){
cout<<"Node "<<(i+1)<<" is connected with ["<<(at(i).links_to[j]+1)<<"]"<<endl;
}
cout<<"For Node: "<<(i+1)<<"\t"<<"Total links: "<<at(i).links_to.size()<<endl;
}
}
int main() {
srand48(46574621);
Links network;
network.CreateLinks(10,1); //(nodes,minimum value of link)
network.OutputNodes();
return 0;
}
Which generate the result like this-
Node 1 is connected with [2]
Node 1 is connected with [10]
For Node: 1 Total links: 2
Node 2 is connected with [1]
Node 2 is connected with [3]
Node 2 is connected with [4]
Node 2 is connected with [5]
Node 2 is connected with [6]
Node 2 is connected with [7]
Node 2 is connected with [8]
For Node: 2 Total links: 7
Node 3 is connected with [2]
For Node: 3 Total links: 1
Node 4 is connected with [2]
For Node: 4 Total links: 1 ... etc
I would like to add a function so that it groups the similar points and gives the output of the total links for each groups. How can I do that?
Updated in response to the answer of Pixelchemist
Let's say I store the data in a file name "MyLinks.txt" like this-
1 2
1 10
2 1
2 3
2 4
2 5
2 6
2 7
2 8...etc
And get the input from the file. Here is the code-
int main (void)
{
ifstream inputFile("MyLinks.txt");
double Temp[2];
Links links_object;
while (true) {
for (unsigned i = 0; i < 2; i++){
inputFile>>Temp[i];
}
for (size_t i(0u); i<10; ++i)
{
links_object.add(Node());
}
links_object.link_nodes(Temp[0], Temp[1]);
/*
links_object.link_nodes(0u, 9u);
links_object.link_nodes(1u, 2u);
links_object.link_nodes(1u, 3u);
links_object.link_nodes(1u, 4u);
links_object.link_nodes(1u, 5u);
links_object.link_nodes(1u, 6u);
links_object.link_nodes(1u, 7u);
links_object.link_nodes(7u, 8u);
*/
}
std::vector<size_t> linksum;
for (auto const & node : links_object.nodes())
{
size_t const linksum_index(node.links().size()-1u);
if (linksum.size() < node.links().size())
{
size_t const nls(node.links().size());
for (size_t i(linksum.size()); i<nls; ++i)
{
linksum.push_back(0u);
}
}
for (auto linked : node.links())
{
linksum[linksum_index] += linked->links().size();
}
}
for (size_t i(0u); i<linksum.size(); ++i)
{
std::cout << "Sum of secondary links with " << i+1;
std::cout << "-link nodes is: " << linksum[i] << std::endl;
}
}
Updated my code,store the results of 'connection' in a text file and trying to get the values from that. But now it gives me the segmentation fault. How can I fix it?

I would use a map. The number of links would be the key and its value would be a vector containing the IDs of nodes with that number of links.
typedef std::map<size_t,std::vector<size_t> SimilarNodeMap;
SimilarNodeMap myMap;
... // fill up the map
for (SimilarNodeMap::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
std::cout << "Nodes with " it->first << " links: ";
for ( size_t i = 0; i < second->size(); ++i )
{
std::cout << second->at(i) << std::endl;
}
}

You can go through the nodes that are part of the "pair" and put them into a list. If there is an element you are trying to add that's already in the list don't add it.(e.x if statement check) Then after going through all the elements check the list size and that should be your links.
Correct me if this isn't what you are asking.
I'm sure there is a better way to do this. The complexity of this is O(n^2) time i believe.

I'd use a std::vector<size_t> where the index of the vector is the number of links of the respective node type.
You iterate over all of your nodes and increment the std::vector<size_t>-entry corresponding to the number of links of this node with the number of links of all nodes that are linked to the current one.
This code:
#include <vector>
#include <stdexcept>
class Node
{
std::vector< Node const * > m_links;
public:
Node(void) { }
void link_to (Node const &n)
{
m_links.push_back(&n);
}
std::vector< Node const * > const & links (void) const
{
return m_links;
}
};
class Links
{
std::vector<Node> m_nodes;
public:
void add (Node const &node) { m_nodes.push_back(node); }
void link_nodes (size_t node_a, size_t node_b)
{
size_t ns(m_nodes.size());
if (node_a >= ns || node_b >= ns)
{
throw std::logic_error("Requested invalid link.");
}
m_nodes[node_a].link_to(m_nodes[node_b]);
m_nodes[node_b].link_to(m_nodes[node_a]);
}
std::vector<Node> const & nodes (void) const
{
return m_nodes;
}
};
int main (void)
{
Links links_object;
for (size_t i(0u); i<10; ++i)
{
links_object.add(Node());
}
links_object.link_nodes(0u, 1u);
links_object.link_nodes(0u, 9u);
links_object.link_nodes(1u, 2u);
links_object.link_nodes(1u, 3u);
links_object.link_nodes(1u, 4u);
links_object.link_nodes(1u, 5u);
links_object.link_nodes(1u, 6u);
links_object.link_nodes(1u, 7u);
links_object.link_nodes(7u, 8u);
std::vector<size_t> linksum;
for (auto const & node : links_object.nodes())
{
size_t const linksum_index(node.links().size()-1u);
if (linksum.size() < node.links().size())
{
size_t const nls(node.links().size());
for (size_t i(linksum.size()); i<nls; ++i)
{
linksum.push_back(0u);
}
}
for (auto linked : node.links())
{
linksum[linksum_index] += linked->links().size();
}
}
for (size_t i(0u); i<linksum.size(); ++i)
{
std::cout << "Sum of secondary links with " << i+1;
std::cout << "-link nodes is: " << linksum[i] << std::endl;
}
}
Prints:
Sum of secondary links with 1-link nodes is: 39
Sum of secondary links with 2-link nodes is: 16
Sum of secondary links with 3-link nodes is: 0
Sum of secondary links with 4-link nodes is: 0
Sum of secondary links with 5-link nodes is: 0
Sum of secondary links with 6-link nodes is: 0
Sum of secondary links with 7-link nodes is: 9
You should get the idea.

You might iterate over all nodes and count.
Pseudo code:
std::map<std::size_t, std::size_t> counter;
for each node
++counter[node.links().size]

Related

C++ permutation tree

I have tasks and I want to calculate the most profitable order to arrange them.
Instead of checking every permutation and doing n*n! calculations, I want to build a tree of permutations, that is, the number of children at each level decreases by 1, and at each node the sub-permutation that has already been calculated will be saved and not recalculated.
For example, if I have 4 tasks, the tree will look like this:
My attached code is missing. I don't know how to build the tree and the give nodes the indexes as in the figure. I know how to deal with a binary tree, but not with a tree where the number of children is different at each lavel.
(The value of each task depends on its location.
I know how to do that, so I didn't include it in the question).
int n = 4;
struct node
{
int task_index = -1;
double value;
struct node **next;
};
void build_tree(node *current_node, int current_level = 0)
{
if (current_level < 1 || current_level >= n)
return;
// current_node->task_index = ? ;
current_node->next = new node *[n - current_level];
for (int i = 0; i < n - current_level; i++)
{
build_tree(current_node->next[i], current_level + 1);
}
}
void print_tree(node *current_node, int current_level = 0)
{
// print indexes
}
void delete_tree(node *current_node, int current_level = 0)
{
// delete nodes
}
int main()
{
struct node *root = new node;
build_tree(root);
print_tree(root);
delete_tree(root);
delete root;
return 0;
}
void build_tree(node *current_node, int current_level = 0)
{
if (current_level < 1 || current_level >= n)
return;
// current_node->task_index = ? ;
current_node->next = new node *[n - current_level];
for (int i = 0; i < n - current_level; i++)
{
build_tree(current_node->next[i], current_level + 1);
}
}
When called with the default parameter of current_level = 0, as you illustrate in your code below, this function exits on the first line without doing anything. You need to decide whether you are indexing starting from 0 or from 1.
Other than that, the general outline of the algorithm looks okay, although I did not explicitly check for correctness.
Now, more broadly: is this an exercise to see if you can write a tree structure, or are you trying to get the job done? In the latter case you probably want to use a prebuilt data structure like that in the boost graph library.
If it's an exercise in building a tree structure, is it specifically an exercise to see if you can write code dealing with raw pointers-to-pointers? If not, you should work with the correct C++ containers for the job. For instance you probably want to store the list of child nodes in a std::vector rather than have a pointer-to-pointer with the only way to tell how many child nodes exist being the depth of the node in the tree. (There may be some use case for such an extremely specialized structure if you are hyper-optimizing something for a very specific reason, but it doesn't look like that's what's going on here.)
From your explanation what you are trying to build is a data structure that reuses sub-trees for common permutations:
012 -> X
210 -> X
such that X is only instantiated once. This, of course, is recursive, seeing as
01 -> Y
10 -> Y
Y2 -> X
If you look at it closely, there are 2^n such subtrees, because any prefix can have any one of the n input tasks used or not. This means you can represent the subtree as an index into an array of size 2^n, with a total footprint O(n*2^n), which improves on the vastly larger >n! tree:
struct Edge {
std::size_t task;
std::size_t sub;
};
struct Node {
std::vector<Edge> successor; // size in [0,n]
};
std::vector<Node> permutations; // size exactly 2^n
This will have this structure:
permutations: 0 1 2 3 4 ...
|-^
|---^
|-------^
|---^
|-^
Where the node at, e.g., location 3 has both task 0 and 1 already used and "points" to all (n-2) subtrees.
Of course, building this is not entirely trivial, but it compressed the search space and allows you re-use results for specific sub-trees.
You can build the table like this:
permutations.resize(1<<n);
for (std::size_t i = 0; i < size(permutations); ++i) {
permutations[i].successor.reserve(n); // maybe better heuristic?
for (std::size_t j = 0; j < n; ++j) {
if (((1<<j) & i) == 0) {
permutations[i].successor.push_back({j,(1<<j)|i});
}
}
}
Here is a live demo for n=4.
The recursive way to generate permutations is if you have n items then all of the permutations of the items are each of the n items concatenated with the permutations of the n-1 remaining items. In code this is easier to do if you pass around the collection of items.
Below I do it with an std::vector<int>. Once using a vector it makes more sense to just follow the "rule of zero" pattern and let the nodes have vectors of children and then not need to dynamically allocate anything manually:
#include <vector>
#include <algorithm>
#include <iostream>
struct node
{
int task_index = -1;
double value;
std::vector<node> next;
};
std::vector<int> remove_item(int item, const std::vector<int>& items) {
std::vector<int> output(items.size() - 1);
std::copy_if(items.begin(), items.end(), output.begin(),
[item](auto v) {return v != item; }
);
return output;
}
void build_tree(node& current_node, const std::vector<int>& tasks)
{
auto n = static_cast<int>(tasks.size());
for (auto curr_task : tasks) {
node child{ curr_task, 0.0, {} };
if (n > 1) {
build_tree(child, remove_item(curr_task, tasks));
}
current_node.next.emplace_back(std::move(child));
}
}
void print_tree(const node& current_node)
{
std::cout << "( " << current_node.task_index << " ";
for (const auto& child : current_node.next) {
print_tree(child);
}
std::cout << " )";
}
int main()
{
node root{ -1, 0.0, {} };
build_tree(root, { 1, 2, 3 });
print_tree(root);
return 0;
}

if user enter 5 then display the last 5 elements of linked list vise versa

i am stuck in my uni assignment....
i have an linked list of 20 elements, i have to take the value from user and if user enter 5 then print the last 5 elements of linked list
void traverse(List list) {
Node *savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i = 1; list.next() == true; i++)
{
std::cout << "Element " << i << " " << list.get() << endl;
}
list.currentNode = savedCurrentNode;
}
im trying this but this method prints all the elements of my linked list
For what little code you have, a review:
// Why are you passing the list by value? That is wasteful.
void traverse(List list) {
// I don't see you taking a value anywhere; surely you know how to do that
// What is happening here? Can't you just assign the head to something
// directly?
Node *savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
// Like you said, this traverses the entire list, it's also poorly
// formed. You literally don't need i.
// for (; list.next(); /* However your list increments here */)
for(int i = 1; list.next() == true; i++)
{
std::cout << "Element " << i << " " << list.get() << endl;
}
// What is the purpose of this?
list.currentNode = savedCurrentNode;
}
For someone who is writing a linked list, this code seems to be fundamentally flawed. My expectation of someone tackling a linked list is that they are about to stop being a beginner, but I'm not seeing that here in the code and what structure of the list class is implied. The list class is weird to say the least.
And just to be clear, my expectation stems from where I place the linked list assignment in my curriculum. It's also more idiomatic than this list.
With that out of the way, this task is trivial if you took the time to think the project through. Most students skip the planning step and create unnecessary headaches for themselves.
Knowing that you would need the total size of the list, why not just make it member data? Any function that adds to the list will increment the value accordingly. And any function that subtracts from the list will decrement accordingly. That way you always know the size of the list at all times.
Knowing the size of the list is most of the battle. You then need to do the arithmetic necessary to advance in the list to satisfy your requirement. And now you can print.
#include <iostream>
class SList {
public:
SList() = default;
//
// Rule of 5 intentionally left out
//
void push_front(int val) {
m_head = new Node{val, m_head};
++m_size; // The magic happens here
}
std::size_t size() const { return m_size; }
void traverse_last(int numElements, std::ostream& sout = std::cout) const {
int placement = m_size;
Node* walker = m_head;
// Move our walker node the appropriate amount of steps
while (walker && placement > numElements) {
walker = walker->next;
--placement;
}
// Now that we're in position, we can print
while (walker) {
sout << walker->data << ' ';
walker = walker->next;
}
sout << '\n';
}
private:
struct Node {
int data;
Node* next = nullptr;
};
Node* m_head = nullptr;
std::size_t m_size = 0ULL;
};
int main() {
SList test;
for (int i = 5; i > 0; --i) {
test.push_front(i);
}
std::cout << "Size: " << test.size() << '\n';
for (int i = 1; i <= 5; ++i) {
test.traverse_last(i);
}
test.traverse_last(10);
}
Output:
❯ ./a.out
Size: 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
1 2 3 4 5
void traverse(List list, int printFrom)
{
Node *savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i=1; list.next(); i++)
{
if(i > printFrom)
{
cout << "Element " << (i - printFrom) << " " << list.get() << endl;
}
}
list.currentNode = savedCurrentNode;
}
solved my prblem by this there printFrom is a variable whose value is number of elemenets that skipped like if my linked list have size of 20 and user want to see last 5 then printFrom stores 15 and skipped 15 values and print last 5

Overwriting node references when cycling through a vector with nodes (C++)

I'm currently solving old exam questions for school and ran into trouble with handling references and pointers. The task is to determine if any number 'n' is divisible by 7 using a graph, nodes and different arcs.
Graph to determining divisibility of number 'n' by 7
In the task I was given a node structure:
struct Node {
int value;
Node * grey_line;
Node * red_line;
};
I was also given a template code for a class 'Mod7' which I have written code in:
class Mod7 {
public:
Mod7() {
// Create the graph here
for (int i {}; i<7; i++) {
nodes.push_back(Node{i});
}
}
int remainder(string number) {
// change this member function to make the test pass
//The n_vector contains 2, 3 and 4.//
vector<int> n_vector = string_to_vector_int(number);
Node * current_node {&nodes.at(0)};
int counter{};
for (int steps : n_vector) {
do { //step along the grey line//
cout << "no Segmentation fault up to this point" << endl;
current_node = current_node -> grey_line;
cout << "Segmentation fault occurs here on the 2nd iteration of the while loop" << endl;
steps--;
} while (steps < 1);
counter++;
//Don't follow the red line if this was the last digit in v//
if (counter == n_vector.size()) {
return current_node -> value;
}
//step along the red line//
current_node = current_node -> red_line;
}
return current_node -> value;
}
// Insert your help functions here
void set_arcs() {
//set grey lines//
for (int i{}; i < nodes.size(); i++) {
nodes.at(i).grey_line = &nodes.at( (i + 1) % 7);
}
//set red lines//
nodes.at(0).red_line = &nodes.at(0);
nodes.at(1).red_line = &nodes.at(3);
nodes.at(2).red_line = &nodes.at(6);
nodes.at(3).red_line = &nodes.at(2);
nodes.at(4).red_line = &nodes.at(5);
nodes.at(5).red_line = &nodes.at(1);
nodes.at(6).red_line = &nodes.at(4);
}
vector<Node> get_node_vector() {
return nodes;
}
private:
// Insert extra data members if needed
vector<Node> nodes{};
};
The Main function looks like this:
int main() {
string test1{"243"};
Mod7 modder{};
//A print-out I made just to make sure I created the nodes okay//
for (Node n : modder.get_node_vector()) {
cout << n.value << " <- Node" << endl;
}
// The examiner's required testing//
if (modder.remainder("243") == 5) {
cout << "Correct" << endl;
}
if (modder.remainder("11111111111111111111111111111") == 2) {
cout << "Correct" << endl;
}
}
The issues i have is I keep getting Segmentation fault which I think is due to the fact that I'm assigning new values to the current nodes' arcs. I don't know really how to continue from here, and I would greatly appreciate any help.
I don't know if the output is particularly giving, but I'll include it too:
> $ ./a.out
> 0 <- Node
> 1 <- Node
> 2 <- Node
> 3 <- Node
> 4 <- Node
> 5 <- Node
> 6 <- Node
> no Segmentation fault up to this point
> Segmentation fault occurs here on the 2nd iteration of the while loop
> Segmentation fault (core dumped)
UPDATE: I just now noticed that the member function 'set_arcs()' was never called. I added a call to it after the for-loop in the class' constructor.
Mod7() {
// Create the graph here
for (int i {}; i<7; i++) {
nodes.push_back(Node{i});
}
set_arcs();
}
Now the program get stuck in the while-loop. So I guess I'm still having issues with overwriting pointers.

C++ How to find the longest possible combination of decreasing numbers in an array

I am working on a problem in which I'm given a list of numbers representing the diameter of cake layers (for example: 9 12 10 7 4 6 11 5). With this list, I have to find the length of the longest combination of numbers that are equal to or decreasing (stacking cake layers from greatest diameter at the bottom to smallest at the top). You are allowed to skip over numbers, but you can't come back to them. I.e. with the previous list, the length of the longest combination would be 5 with the combination being (12,10,7,6,5).
I believe that the best way to solve this would be feeding the array into a tree and returning the height of the tree. This is currently the code I have, with a working tree implementation above the main
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string sizeInput, transfer; //Strings to hold input and transfer to array
int maxLayers = 0, numOfInputs = 0, numNodes = 0; //ints for holding the max height and the number of inputs by the user
int cakeSizes [30]; //Array holding sizes of the cakes input, no more than 30
cout << "Cake sizes: ";
getline(cin,sizeInput); //Gets input from user and puts into a stringstream
stringstream readInput(sizeInput);
while(readInput >> transfer)
{
cakeSizes[numOfInputs] = stoi(transfer); //Puts the numbers into the array and counts how many were placed
numOfInputs++;
}
for(int i=0; i<numOfInputs; i++) //Puts the array into a tree
{
Tree<int> cakeStack; //Creates tree to hold combination
initialize(cakeStack);
for(int j=i; j<numOfInputs; j++)
{
if(cakeSizes[j]<=cakeSizes[j-1])
{
insert(cakeStack, cakeSizes[j]);
}
}
if(height(cakeStack) > maxLayers) //Checks if the new combination tree's height is greater than the last
{
maxLayers = height(cakeStack);
}
destroy(cakeStack); //Destroys the tree from the previous combination in preparation for new one
}
cout << endl << "You can build a cake with " << maxLayers << " layers.";
}
This actually works for combinations that are always decreasing (like 5,4,2,1 and 8,3,2,1), but it fails when interrupting numbers are thrown in (like with 5,4,2,8,1). I'm almost certain that the problem lies here:
for(int j=i; j<numOfInputs; j++)
{
if(cakeSizes[j]<=cakeSizes[j-1])
{
insert(cakeStack, cakeSizes[j]);
}
}
But I'm unsure of how to implement it an a way that checks all combinations of the array (like skipping numbers that wouldn't give the longest combination), rather than running straight down the list unable to skip numbers.
The tree is definitely the way to go. You build the tree by inserting each value under the smallest node larger than it. Then when the tree is finished you iterate through it looking for the longest path.
What I did in the code below is I made a head node to store the sub trees and it needed a really large value so that all the inputs would fit under it. But then when I print the tree or look for a path I need to ignore that head node, so I have to keep track of the depth.
#include <iostream>
#include <vector>
#include <climits>
struct Tree {
Tree(int value) : value(value) {}
int value;
std::vector<Tree> children;
};
// Recursively check this level of the tree
void insert_node(Tree& node, int value)
{
// if the new value is bigger than where
// we are then stop descending
if (value > node.value)
return;
// if the new value fits under this
// parent then check all the children
bool inserted = false;
for (Tree& child : node.children)
// if we find a child large enough
// then insert ourselves inside
if (value < child.value)
{
insert_node(child, value);
inserted = true;
}
// if the new value fits under this parent but
// not under any of the children then put it here
if (!inserted)
node.children.push_back(value);
}
void print_tree(Tree node,
std::vector<bool> flags = std::vector<bool>(100, true),
bool last = false,
int depth = 0)
{
for (int i = 1; i < depth; ++i)
{
if (flags[i])
std::cout << "| ";
else
std::cout << " ";
}
// Don't print our fake head
if (depth > 0)
{
std::cout << "+- " << node.value << '\n';
if (last) flags[depth] = false;
}
int n = 0;
for (Tree child : node.children)
{
last = (n++ == node.children.size() - 1);
print_tree(child, flags, last, depth + 1);
}
flags[depth] = true;
}
void print_path(std::vector<int> path)
{
std::cout << "Path:";
for (int value : path)
std::cout << " " << value;
std::cout << "\n";
}
void print_paths(Tree node,
std::vector<int>& max_path,
std::vector<int> path = std::vector<int>(),
int depth = 0)
{
// Don't add our fake head
if (depth > 0)
path.push_back(node.value);
if (node.children.size() == 0)
{
print_path(path);
// check if this path is the longest one yet
if (max_path.size() < path.size())
max_path = path;
}
for (Tree child : node.children)
print_paths(child, max_path, path, depth + 1);
}
int main()
{
Tree head(INT_MAX);
std::vector<int> input = {9, 12, 10, 7, 4, 6, 11, 5};
// Build the tree
for (int value : input)
insert_node(head, value);
// Print the tree
std::cout << "Tree:\n";
print_tree(head);
std::cout << "\n";
// Print the paths and
// find the longest one
// and then print it too
std::vector<int> max_path;
print_paths(head, max_path);
std::cout << "\nLongest ";
print_path(max_path);
return 0;
}

algorithm implementation error (DFS)

I was trying to implementing dfs to print paths from starting node . I followed algorithm from Coremen 's book . Here is my code :
DFS
#include<iostream>
#include<stack>
using namespace std;
int vertex,edge,source,time,adjacency_matrix[100][100],parent[100],Distance[100],Finishing_time[100];
string color[100];
stack<int> result;
void inputGraph();
void initialize();
void doDFS();
void doDFSvisit(int);
void printPath();
//void printAll();
//void printAdjacencyMatrix();
//void printColor();
//void printDistance();
//void printParent();
int main(void)
{
inputGraph();
//initialize();
doDFS();
printPath();
//printAll();
return 0;
}
void inputGraph()
{
cout<<"Total vertex : ";
cin>>vertex;
cout<<"Total edge : ";
cin>>edge;
int i,j;
for(i=1; i<=edge; i++)
{
int start,finish;
cout<<"Enter start and end node for edge "<<i<<" : ";
cin>>start;
cin>>finish;
adjacency_matrix[start][finish]=1;
}
cout<<"The adjacency matrix is : "<<endl;
for(i=1; i<=vertex; i++)
{
for(j=1; j<=vertex; j++)
{
cout<<adjacency_matrix[i][j]<<" ";
}
cout<<endl;
}
}
void initialize()
{
cout<<"Enter source node : ";
cin>>source;
}
void doDFS()
{
int i,j;
for(i=1;i<=vertex;i++)
{
color[i]="white";
parent[i]=0;
}
time=0;
for(i=1;i<=vertex;i++)
{
if(color[i]=="white")
{
doDFSvisit(i);
}
}
}
void doDFSvisit(int node)
{
int i;
time=time+1;
Distance[node]=time;
color[node]="grey";
for(i=1;i<=vertex;i++)
{
if(adjacency_matrix[node][i]==1)
{
if(color[i]=="white")
{
parent[i]=node;
doDFSvisit(i);
}
}
}
color[node]="black";
//extra line for result
result.push(node);
//
time=time+1;
Finishing_time[node]=time;
}
void printPath()
{
cout<<"Path :"<<endl;
int i;
for(i=0;i<=result.size();i++)
{
cout<<result.top()<<" -> ";
result.pop();
}
cout<<" End"<<endl;
}
My problem :
for input :
6
6
1 2
1 4
2 3
3 4
5 3
5 6
my output should be :
5 6 1 2 3 4 end
but my output is :
5 6 1 2 end
it seems printing values from stacks creates problem . please correct me where i did mistake , Thanks in advance .
[ P.S. : Pic of the directed graph that I used for input , http://imgur.com/fYsICiQ ]
There is mistake in the print_path function.
Your for-loop termination condition checks result(stack)'s size which decrements each loop-iteration by pop calling.
Your print_path function should look like something like this:
void printPath(){
cout<<"Path :"<<endl;
int i;
while(!result.empty()){
cout << result.top() << " -> ";
result.pop();
}
cout<<" End"<<endl;
}
Additionally consider this DFS implementation:
list<size_t> l[N];
bool used[N];
void DFS(size_t s){
if (used[s])
return;
used[s] = true;
for(auto i = l[s].begin(); i != l[s].end(); i++)
if(!used[*i]){
DFS(*i);
}
}
used is global bool array indicating i'th vertex is visited or not. We have no need to color vertexes. We have to know is it already visited or not.
l is adjacency list (see http://www.geeksforgeeks.org/graph-and-its-representations/ )
We run DFS on some vertex.
If it's visited we do nothing.
Else we mark this vertex as visited. And then go deeper running DFS on each vertex adjacent current vertex.
For more information about DFS see https://en.wikipedia.org/wiki/Depth-first_search
Here's how I would implement DFS in C++. First some observations:
I'll use adjacency lists (std::vectors) rather than an adjacency matrix.
Nodes aren't owned by their neighbors. They're assumed to be owned by a parent Graph object.
So, without further ado:
struct Node {
std::vector<Node *> neighbors;
// Other fields may go here.
}
void process(Node * node)
{
// Actual logic for processing a single node.
}
// Of course, in idiomatic C++, this would be a template
// parameterized by a function object, rather than contain
// a hard-coded call to a fixed `process` function.
void depth_first(Node * start)
{
std::stack <Node *> pending = { start };
std::unordered_set<Node *> visited;
while (!pending.empty()) {
Node * current = pending.pop();
process(current);
for (Node * neighbor : current->neighbors)
if (visited.find(neighbor) == visited.end()) {
pending.push (neighbor);
visited.insert(neighbor);
}
}
}
A nice thing about this implementation is that, in order to get BFS, you only need to replace std::stack with std::queue, and leave the rest of the code exactly as is.