473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

recursive calls to function. how should i do it?

hello..
i have the following problem to solve, which is part of mst problem.
i have some nodes (ie cities if you like), and each city is connected
with some other. we don't know which city is connected with what other
city, except for the 1st level (direct connections from city to city).
i want to build the complete map of what is connected to what.

let's say that i have:
city A, connected to: D, F
city B, connected to: A, C,
city C, connected to: D,

starting from city D, i find out that it's connected to C.
C is also connected to B and directly. We set also true that D is
connected to B.
Since B is connected To A, D must know that it's also connected to
whichother cities A is connected with.
And So on..

As you can see, there is a recursive "thing" in this process.
I 'm wondering how to implement it.. I thought of building a function
that would recursively calls its self, and does what i described with
words above.
And for each step deeper it will go, it won't check if the current
node is also connected with past-checked nodes..

I hope you get what i mean.. Can't describe it better :|

If you think that it could be done better/easier with another way,
tell me! :)

Feb 23 '07 #1
6 1745
On Feb 23, 12:58 pm, "streamkid" <stream...@gmail.comwrote:
i have some nodes (ie cities if you like), and each city is connected
with some other. we don't know which city is connected with what other
city, except for the 1st level (direct connections from city to city).
i want to build the complete map of what is connected to what.

let's say that i have:
city A, connected to: D, F
city B, connected to: A, C,
city C, connected to: D,

starting from city D, i find out that it's connected to C.
C is also connected to B and directly. We set also true that D is
connected to B.
Since B is connected To A, D must know that it's also connected to
whichother cities A is connected with.
And So on..

If you think that it could be done better/easier with another way,
tell me! :)
Common ways to solve this are Dijkstra's algorithm, and the Floyd-
Warshall algorithm. Both are covered in Wikipedia.

Michael
Feb 23 '07 #2
streamkid wrote:
If you think that it could be done better/easier with another way,
tell me! :)
BTW, this is not really a C++ question but hey, its an interesting
problem. So here is how I would approach this:
- write a recursive (or not) depth first traversal given a node
and returns all reachable nodes from that node. The signature
might look like:
{set of all reachable nodes from a and including a} dft( node a );

- then I would approach the problem by first initializing all nodes
marked as unreachable. Then pick any unreachable node and call dft
on it.

- this returns a set of nodes that are accesssible to and from each
other. Mark your nodes properly. eg. given {a, b, c} then
a can go to b and c
b can go to a and c
c can go to a and b

- rinse and repeat for the remaining unreachable nodes

Good Luck with your project :)

Feb 23 '07 #3
thanx both of you for your answers :)

i started solving the mst, by using the reverse-delete algorithm..
" * Start with graph G, which contains a list of edges E.
* Go through E in decreasing order of edge weights.
* Check if deleting current edge will further disconnect graph.
* If G is not further disconnected, delete the edge.
"

the function i want to write is for step 3 :)
i though of a recursive function and i 've written some lines..
maybe i post code tonight (local time 1am atm) or 2morrow morning :)

thanx again for your answers :)
streamkid

Feb 23 '07 #4
streamkid wrote:
thanx both of you for your answers :)

i started solving the mst, by using the reverse-delete algorithm..
" * Start with graph G, which contains a list of edges E.
* Go through E in decreasing order of edge weights.
* Check if deleting current edge will further disconnect graph.
* If G is not further disconnected, delete the edge.
"

the function i want to write is for step 3 :)
i though of a recursive function and i 've written some lines..
maybe i post code tonight (local time 1am atm) or 2morrow morning :)

thanx again for your answers :)
streamkid
BTW, Wikipedia is your friend. It even has pseudocode! Imagine that!

http://en.wikipedia.org/wiki/Reverse-Delete_algorithm
Feb 23 '07 #5
On Feb 24, 1:23 am, Piyo <cybermax...@yahoo.comwrote:
streamkid wrote:
thanx both of you for your answers :)
i started solving the mst, by using the reverse-delete algorithm..
" * Start with graph G, which contains a list of edges E.
* Go through E in decreasing order of edge weights.
* Check if deleting current edge will further disconnect graph.
* If G is not further disconnected, delete the edge.
"
the function i want to write is for step 3 :)
i though of a recursive function and i 've written some lines..
maybe i post code tonight (local time 1am atm) or 2morrow morning :)
thanx again for your answers :)
streamkid

BTW, Wikipedia is your friend. It even has pseudocode! Imagine that!

http://en.wikipedia.org/wiki/Reverse-Delete_algorithm
yeah, i know, that's my source. i didn't know about that algorithm
before i read wiki ;)

Feb 24 '07 #6
On Feb 24, 1:23 am, Piyo <cybermax...@yahoo.comwrote:
streamkid wrote:
thanx both of you for your answers :)
i started solving the mst, by using the reverse-delete algorithm..
" * Start with graph G, which contains a list of edges E.
* Go through E in decreasing order of edge weights.
* Check if deleting current edge will further disconnect graph.
* If G is not further disconnected, delete the edge.
"
the function i want to write is for step 3 :)
i though of a recursive function and i 've written some lines..
maybe i post code tonight (local time 1am atm) or 2morrow morning :)
thanx again for your answers :)
streamkid

BTW, Wikipedia is your friend. It even has pseudocode! Imagine that!

http://en.wikipedia.org/wiki/Reverse-Delete_algorithm
" * Start with graph G, which contains a list of edges E.
* Go through E in decreasing order of edge weights.
* Check if deleting current edge will further disconnect graph.
* If G is not further disconnected, delete the edge.
"
which i pasted before is from wiki, but my problem is to implement 3rd
step (* Check if deleting current edge will further disconnect graph.)
in c++...

Feb 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
1
by: skanemupp | last post by:
the 0.409 vs 0.095 is the total times right? so the imperative function is >4 times faster than the recursive. or what does tottime stand for? is this always the case that the recursive function...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.