I'm currently preparing an exam and I'm having some troubles implementing Breadth - First -Search algorithm about graphs.
Basically I found an exercise where I've been asked to find the shortest path between a start vertex v1 and an ending vertex v2 using the BFS algorithm. Now, on the web it's easy to find some BFS implementations in C but I'm not able to understand how to adapt it to find only the shortest path.
I've already made all the functions about push and pop in stack list and graph making from user input I'm just asking to one kind person if he can write here an example code in C of the BFS function to solve my problem.
Thank you and have a nice day!
BFS is iterative, and each iteration sets the parent property of each node right? So if one of the vertex-labels in the current iteration matches your target (v2) then you are done. Next you just trace the parent property back to your start (v1)
Related
I want to look at the code for the igraph function distances I'd like to look at the implementation of the Dijkstra algorithm, to see if it is possible to make a change such that as soon as any path, between a fixed source and target, exceeds a threshold then that path is terminated in the search.
I can see that the function calls C_R_igraph_shortest_paths but I can't find this in the igraph package information for R or C on Github. I know the distances function is held in the structural.properties.R file but I haven't got any further than that.
I haven't had to look at this depth before so I may have missed something obvious.
Look at igraph's C interface:
https://igraph.org/c/doc/igraph-Structural.html#idm470927281168
The source is on GitHub:
https://github.com/igraph/igraph/
Look at structural_properties.c.
I'd like to look at the implementation of the Dijkstra algorithm, to see if it is possible to make a change such that as soon as any path, between a fixed source and target, exceeds a threshold then that path is terminated in the search.
Yes, this is possible, and there is an open issue for it. PRs are welcome!
https://github.com/igraph/igraph/issues/720
I'm currently learning C in Uni but for some reason it's just so difficult for me. I couldn't find a simple step by step guide and everything that's on the internet is just so complex and without much explanation.
I'm supposed to write this program:
'Using the binary tree and list write a program that reads a text file and prints to the output file all the words in alphabetical order along with the line numbers in which the word occurs.'
And I just don't know how to start it. I can open files, run it from the command line, but I have no idea how to create a binary tree, get the words from a file and put them there, and then create a list inside the binary tree. All the examples I've found are so different that I don't know how to rewrite them so they would work for me.
Could anyone help? Even a few lines of code that would guide me in the right direction would help so much!
For starters, its binary-search tree,(special kind of binary tree) that is required for the given problem.
A binary search tree, is a binary tree, that is populated with comparable objects, like numbers. Meaning given two numbers x and y, the following three boolean conditions can be answered without any ambiguity.
x greater than y
x less than y
x equal to y
Now a binary search tree is built upon the above boolean conditions. The analogy here is that words are also comparable, which decides there order in a typical oxford dictionary. Like apple < box and therefore, apple comes before box in alphabetical order.
How to get alphabetical order of words?
Once you have populated your tree, a simple inorder traversal will do the rest, that is, listing the words in alphabetical order. Just remember to also have variable for line numbers, which can be stored at the same time you are building your tree, which can later be retrieved while printing words in order.
Take the code as an exercise.
I am looking after an approach for the following task:
given a set of files that are highly similar (I am using Fuzzy hashing here), I would like to know if there is an algorithm that allows to label those files with a version number. The output should return the sequential order of when those files have been generated.
The reason is I have to re-organize data of a team who were not familiar with version control.
Thank you
A fairly simple approach (I hope) would be to try and convert this into some kind of graph problem.
Let's say every file is a node with edges between every two files.
The weight of an edge between two nodes would be, for instance, the number of different lines between the files (or some some other function).
What you do next is find a non-cyclic path that traverses all files with the minimum cost. something like this, if you know the first file and the last.
You could add an empty file and the latest version you have as your start and end nodes.
I'm guessing this won't give you the exact result, but it'll probably give you a good starting point.
Hope this is helpful.
I'm am writing a code to find the shortest distance between two points. My code is working perfect till now. I mean it finds the distance and the path that they should pass. I need to print this information, but I should make a print function. The way it works is something like that: For example initial point is 4 and final is 13.
I should come up with an algorithm that check their intermediate points. Let's say between 4 & 13 there is point : 7
4--7--13 Now I need to check every point between them like:
4--6--7--9--13 To be more specific it will check if there is a point between 4-6 and 6-7 and 7-9 and 9-13. So next in the next iteration it may be formed another list like:
4--2--6--7--5--9--17--13 Now let's say that there will not be any intermediate value between them. And that is what I should print. I really would appreciate any help, suggestion that you may give to me
The Warshall-Floyd algorithm (used by the OP), has a version which is able to determine the path in addition to the distance between nodes of a graph:
Floyd-Warshall algorithm with path-reconstruction
However, it must be noted that this is not the best possible algorithm to solve the shortest-path problem.
This sounds like recursion would be the best way to do this. If it already can find the shortest path, im assuming you have a function written to find the shortest path between 2 points. Maybe you could recursively break down the list, find the shortest path and append that point to a list.
Edit, sorry i misread your question, you need to find the midpoint. Pass a recursive function the whole list of points and find a midpoint. If one exists, add it to a list. If there is no midpoint dont append anything. Continue calling this function until you come to the base case, which should be 1 or 2 points in the list
I am new here and I have a problem that's bugging me.I am a beginner so please don't laugh at me.
I want to make recursive quicksort work on a large number of elements,let's say 100000.I know this will cause the stack to overflow.I have been googling for the past few days trying to find a way to manage the call stack.I can't really find a good source of information.
My ideea is to remove the return adress of each recursive call,except the last one,which will return to the first function call.I don't know if that is possible or if it is another solution for this problem.
P.S. : I want to keep the quicksort recursive.
Sorry if my problems looks silly,but i sould appreaciate any pertinent answer.
Sorry for my bad English.
Thank you!
The standard way to solve the issue of running out of stack space with recursive algorithm is to implement it iteratively instead.
Please note that 100000 items in an array is nothing; this will only lead to nested calls 17 functions deep:
$ echo "l(100000)/l(2)" | bc -l
16.60964047443681173951
That's log(N)/log(2) -- the log(2) is to convert it to log base 2.
Any platform that supports recursive function calls will almost certainly be able to handle 17 nested calls.
If stack space is a problem but memory in general isn't, you can easily convert a recursive implementation into an iterative one by using your own heap-allocated stack. That is, instead of making a recursive function call, push the arguments you care about onto your own stack data structure. You then can iterate over your stack and process each set of arguments.
it sounds like you're trying to do tail recursion, which has been discussed here;
Tail recursion in C