Graph algorithm question

Hello.
I am trying to find a graph algorithm (by graph I mean nodes interconnected to each other,not the sketch of a function).This algorithm should yield the route that passes from as many nodes of the graph as possible,though it is forbidden to step on a node twice,and then ends back to the first node that it started from,thus forming a cycle.
I am positive that this algorithm must have been researched by now,so in order to avoid thinking it myself I wonder if someone would know to direct me somewhere in the web where I can get it.Since I am a medical student,any terminology help would be also appreciated(as to the name of what I want,keywords to search etc).

OK. Before you get too geeky here, try a brute force approach. if your graph is small or has limited connectivity, you may not need to get fancy. Its surprising what a computer can do without breaking a sweat. In any case I whipped up a solution as shown below. It is untested and will most likely have bugs (and does not check for null conditions). But as you can see the code is pretty basic and should get you from point A to point B (pun inteneded).     public List<Integer> getLargestCycle( Map<Integer,List<Integer>>graph )
        List<Integer> longestCycle = new Vector<Integer>();
        List<Integer> path = new Vector<Integer>();
        for ( Integer startingPoint : graph.keySet() )
            path.add(startingPoint);
            List<Integer> aCycle = getLargestCycle( graph, path );
            if ( aCycle.size() > longestCycle.size() )
                longestCycle = aCycle;           
        return longestCycle;
    protected List<Integer> getLargestCycle( Map<Integer,List<Integer>>graph,
        List<Integer> currentPath )
        List<Integer> longestCycle = new Vector<Integer>();
        Integer currentPoint = currentPath.get( currentPath.size()-1 );
        Integer startingPoint = currentPath.get( 0 );
        for ( Integer nextPoint : graph.get( currentPoint ) )
            List<Integer> aPath = new Vector( currentPath );
            aPath.add( nextPoint );
            if ( nextPoint.equals( startingPoint ) )
                if ( longestCycle.size() == 0 )
                    longestCycle = aPath;
            else if ( currentPath.contains( nextPoint ) )
                // not a cycle here.
            else
                List<Integer> aCycle = getLargestCycle( graph, aPath );
                if ( aCycle.size() > longestCycle.size() )
                    longestCycle = aCycle;
        return longestCycle;
    }

Similar Messages

  • Graph Theory question...

    My graph knowledge is still requiring maturing. ;) I was wondering if anyone could help me with this question:
    given a set of verticies in an undirected graph, (not all may be connected) I want to determine whether there exists a "walk" from Vi to Vj and if so, I want to work out the shortest "path" between the two.
    I can use a matrix to define my graph view no problems, but how to determine all this computationally is a bit challenging.
    Richie !

    shortest path, as in the "Traveling Sales Person" problem does not require all possible paths to be searched, but it does require setup as if all would be searched (British Museaum style of search using a depth first algorithm). Heuristic clipping of paths can be done once you have an inital path of length L. Any time you reach L length you need not follow that path, nor any of its children. If you reach the destination in shorter than L length, then take the new path as the optimal path length and continue until all paths are exhausted.

  • Graph algorithm concerning colors

    Hi, all.
    Suppose we have a rectangle of width w and height h, which has w*h square grids. At any time each grid can either be empty or be occupied by a colored square. Whenever l squares in a series (horizontal, vertical or diagonal) have the same color, we call them "connected". Note l here is a variable. So I need an algorithm to find out all such connected squares.
    I think it is a question in the graph theory field. Anyone got a hint or an idea for me? Many thanks.

    But I'm still not so clear about how to resolve my
    problem. Suppose BFS is used, do you mean I should
    start at any vertex and search for adjacent vertex
    that has the same color with the current one, and
    iterate like this? If so, I have to apply this
    procedure on each occupied grid. Did I go right? If
    not, could you be more specific?Let's say this is a Grid which exists of a List of Cells.
    [y]
    5 (o)-(o)-(o)-(o)-(o)
        |   |   |   |   |
    4 (o)-(o)-(x)-(o)-(o)
        |   |   |   |   |
    3 (o)-(x)-(x)-(x)-(o)
        |   |   |   |   |
    2 (o)-(o)-(x)-(o)-(o)
        |   |   |   |   |
    1 (x)-(x)-(x)-(x)-(o)
        1   2   3   4   5 [x]A Cell has (at least) the following methods:
    - left(), returns the Cell on the left side of this Cell (or null if x = 1)
    - right(), returns the Cell on the right side of this Cell
    - up(), ...
    - down(), ...
    Say we want to examine all connected Cells with the x-color, starting at (1,1),
    add that first Cell in a List and perform a BFS on the Grid:
    List<Cell> connected = new ArrayList<Cell>();
    connected.add(Grid.getCell(1, 1));
    BFS(connected);A BFS would look a bit like this:
    void BFS(List<Cell> visited) {
      // Get the last Cell in the List
      Cell last = visited.get(visited.size()-1);
      // Get all connected Cells
      Cell left = last.left();
      Cell right = ...
      // If the left Cell is not null, if it's the same color
      // as the last node, and if it's not already in your visited-
      // list: add it and perform a BFS on the new visited-list.
      if(left != null && last.color.equals(left.color) &&
         !visited.contains(left)) {
        visited.add(left);
        BFS(visited);
    }Hope it's of help to you.
    Good luck.

  • Graph scaling question

    Using the graph palette tools on a XY graph having 2 Y scale, I would like to scale the first Y axis without changing the second one. I use LV 6.0.2. Any tips? Thanks

    If you simply right click on the scale in question, you can format that. Just be sure to uncheck the Autoscale axis on the pulldown menu.
    eric
    Eric P. Nichols
    P.O. Box 56235
    North Pole, AK 99705

  • Mouse Scroller Algorithm question

    I have a mouse controlled scroller which I've fashioned from Lee Brimelow's excellent tutorial. I'm at the stage now where I want to add more thumbnails to the scroller's content area, though when I do, the scroller will not scroll any more than 8 thumbnails in either direction.
    My question is (for those of you who are way better at Math, than me) how do I complete the algorithm to compensate for adding more than just 8 thumbnails - into, say, 30 thumbnails and have the scrolling behavior always show the last or first thumbnail when the mouse is at either side of the scroller movieclip?
    this.addEventListener(MouseEvent.MOUSE_MOVE, scrollPanel);
            private function scrollPanel(e:MouseEvent):void {
                var xdist:Number = mouseX - 250;
                //_xContainer is an mc which holds the thumbnail images
                TweenLite.to(_xContainer, 0.3, { x:Math.round(-xdist)});

    when the mouse is at one extreme position the thumbnails should be at an extreme position and when the mouse is at the other extreme the thumbs should be at their other extreme.  then use linear interpolation for all other mouse positions.
    so, for example, if you want mouseX=0 to correspond to the thumbnail parent (_xContainer) being at zero (assuming a left edge reg pt) and you want mouseX=stage.stageWidth to correspond to -_xContainer.width+stage.stageWidth, declare and type your variables and use:
    findParamsF(0,0,stage.stageWidth,stage.stageWidth-_xContainer.width);
    function findParamsF(x1,y1,x2,y2){
    m=(y1-y2)/(x1-x2);
    b=y1-m*x1;
    private function scrollPanel(e:MouseEvent):void {
                var xdist:Number = m*mouseX + b;
                //_xContainer is an mc which holds the thumbnail images
                TweenLite.to(_xContainer, 0.3, { x:Math.round(-xdist)});

  • Advance Graph Algorithm

    Hello guys,
    I am working now on a TSP(traveling salesman problem), I am facing a very challenging problem in fact.
    The Main Problem: I need to find all the possible routes from A to B, I believed that there is no algorithm to provide such thing after a deadly search, so If anyone can help with just a link or a similar algorithm that would be great.
    And if you are interested to know why I need such algorithm;
    1 - I need to have the TSP works on only a part of a big graph, like a big city and I work the TSP only on 4 places.
    2 - So I need to make a small Graph object for the only 4 places, and pass it to TSP.
    3 - I need to make sure that all edges of this graph are set to the shortest paths between every pair of places. But if I found out that this shortest path go through a place which is in the TSP problem, then I have to find another(thats why i need the all routes algorithm).
    4 - (which is the main problem) this short path can't pass through any place which is within the TSP problem(coz TSP go through every place only once).
    (ex if the TSP is interested in places A, B, C, D and the shortest path from A to B passes through D, I can't pass this small graph object to TSP until I find another path connecting A to B and doesn't pass through C nor D -if there is no such path then of course I have to set it with infinity-).
    I hope one of you guys worked on this problem before,
    Thanks & Regrads,
    ES_Coders

    Thanks all,
    I have already solved the problem. I will declare it again here.
    TSP: is to find a cycle that goes through every node only once and goes back to the start point.
    MySystem: consists of nodes from A, B, C, ...., Z.
    Main Idea: user chooses a number of nodes to find their TSP.
    Ex: User wants to find TSP around A, B, C only.
    Problem: if the shortest path (A to B ) go through another node N, and also shortest path(B, C)go through this very same node N. Then the cycle goes through the node N twice which makes the TSP fail.
    Solution:
    1- Use special type of Floyed(or Dijkstra) which finds the shortest paths between every pair of nodes (n1, n2) (n1, n3) ... . But the special part is that this Floyed method takes an array of the nodes considered by TSP lets say{t1, t2, ...}. And it finds the shortest path between (n1, n2) except that this shortest path doesn't go through any of {t1, t2, t3, ...}.
    2- Now when I apply TSP lets say for {A, B, C} I am sure that shortest path from (A to B) doesn't go through node C, but It can go through some other node n which doesn't belong to {A, B, C} -some other node in the system-
    3- After I get my answer from TSP, if i found that this TSP doesn't pass through any place more than once, this is cool, I did it. But if it does pass through the node n more than once. (Here is the trick) I apply TSP for {A, B, C, n} by this way I ll start again whit the special Floyed making sure that every shortest path doesn't pass through n. And when I go for the real TSP the node n won't be passed through twice.
    I hope I was able to declare the problem and the solution,
    Thx a lot guys,
    ES_Coders

  • Simple algorithm question

    declare CHARS string
    declare C integer
    C<-- 0
    CHARS <-- "abc"
    repeat
    CHARS<-- concat(CHARS, CHARS)
    C<-- C + 3
    output C , ":", length(CHARS)
    until ( C > 8)
    What is the output of this algorithm?

    People just get a bit wound up if the OP post a question if it doesn't look like he made any effort to do it himself.
    That bit of psudo-code is not difficult to read. If he had a problem understanding it he could have just asked what does a certain piece mean.
    If he could understand the code then he had the option of either working it out on a page or coding it up but did neither.
    Nobody on this site (few anyway) would get get out a pen and paper or write code to solve what is obviously someones homework. You can't blame people for that.

  • Dual graph algorithm

    Hello to all of you!
    Does anyone know a known algorithm to create the dual graph of a planar (undirected) graph? Or could you give me a hint? I tried to devise a DFS approach, which would use the back and cross edges of the DFS tree in order to form the faces. The problem is that I need the set of disjoint minimum cycles. Thus I would need an algorithm to calculate the difference AUB - A^B, for two cycles A,B, where B is a part of A. By the way, is there any recommended representation of a face?
    Thank you in advance and thaks for your time!
    P.S. For those who would ask what is a face, a face is the area defined by a minimum cycle e.g.:
    In this mesh (ignore dots):
    a-b-c
    I . I . I
    d-e-f
    a,b,c,d form a face
    b,c,e,f form a face
    and there is also the external face -which by the way how can it be defined?
    P.S.2 the Dual Graph is the graph formed by representing each face as a node and each edge which separates two faces, as an edge between the two newlly created nodes.

    OK , guess you're right! Since the following graph:
    O     O
    |\   /|
    | \ / |
    |  O  |
    | / \ |
    |/   \|
    O     O
    and this one
    O---
    |    \
    | O  |
    | | \|
    | |  O
    | | /|
    | O  |
    |   /
    O---are the same, however they have a different embedding AND dual graph.
    So, the embedding is defined by the coordinates of the nodes, along with
    their connectivity info.
    This means that for a start I need an algorithm to return the different faces of
    the graph. (at least these ones have to be fixed in number -no?). Then from
    the topological information for each face I should check their neighboring
    relations.
    For the face discovery I have thought of the following algorithm (in language):
    Create the DFS tree
    -Start from the root r of the DFS tree and visit each neighboring node based
    on the preordering number
    -When we reach a node b which has one or more back-edges we have a
    cycle.
    --For each back edge we go back to the node bs, starting from the
    back-edge which leads us to the node bs with the greatest start time
    (gretest preorder number).
    --from this node, bs, visit neighboring nodes, again based on preordering,
    until we find the node b (which will complete the cycle)
    ---While going towards b follow in reverse direction, each backedge that
    terminates a currntly visited node, and starts from a node t whose
    preorder[t] <= preorder[b]  (to avoid bypassing b by going deeper in the tree)
    and
    postorder[t] > postorder[b] (to avoid reaching another branch)
    -When we reach a node, cs,which has a cross edge to another node, ce, of
    the DFS tree (as a cross edge I mean a (u,v) where pre[u]>pre[v] and
    post[u]>post[v]) we have found another cycle, thus a face
    --from ce we go up the DFS, i.e., in reverse preordering, until we find a node
    a whose postorder[a] > post[cs]
    --from a we go down the DFS (in preordering) choosing the neighbor, an,
    whose postorder[an] >= postorder[cs] (equality means we have found cs
    thus we're done).  While descending we follow the same rules as before, in
    the case of the back edge.Of course the implementation of this algorithms has to support two-way movement in the DFS tree. The distinction between tree,back and cross edges is made through the pre and post ordering.
    I'm not sure but i think that, with the restrictions I have put, this algorithm can be ran while discovering and creating the DFS tree, rather after the DFS tree has been fully created.
    So if the algorithm IS correct, I only need a way to derive the connectivity between the produced faces via their topological information. That is, apart from the indisputable fact that if two faces have two nodes in common, they are neighbors, we have to locate all other edges between faces.
    One idea is for each pair of faces to check their min and max, X and Y coordinates. I have a sense that for faces f1and f2 where,
    f1_max_X > f2_max_X and
    f1_min_X < f2_min_X and
    f1_max_Y > f2_max_Y and
    f1_min_Y < f2_min_Y
    (there must be at most one equality (i.e. <=)but I guess its no harm to add all equalities there)
    holds, f1 and f2 are neighbors, thus an edge (f1,f2) must exist in the dual graph.
    Of course I have written a lot but I really would like to know if there's a "good" algortihm to create faces out there and a technique to create edges between faces given the coordinates.
    I propose the ideas in case someone finds something wrong and reports it or otherwise someone else finds my answer useful in the same topic.
    Thank you in advance!
    Message was edited by:
    M.M.

  • Graph Cursor Question

    I have a cursor on a XY-graph that I'm trying to adjust (x position) via controls on the front panel.  I have one control that works great and adjusts one number at a time (ex. 500 to 501, etc).  I'd like to have two controls one like I have that would be a fine adjustment (I believe) and another for coarse adjustment that would adjust the value by 50 or whatever.  Hence this would be adjusted first (ex 550) and then the other control would be adjusted for fine tuning (ex to say 551 etc). 
    I'm having trouble figuring this out, and was hoping someone could giv eme some tips or ideas as to what to do.  I thought this might be something fairly common, but maybe not.
    Thanks in advance...
    Using Labview 7.0 and 2010 SP1 with Windows XP and 7.

    LabVIEW 8.0 would make these things much easier, because we have events for cursor movements! .
    In LabVIEW 7.0, you need to code around it. For example, you could place the cursor reads into a timeout event which is only active when the mouse is down on the graph (enable timeout with mouse down, disable with mouse up event).
    The attached shows one simple possibility (I don't think you need the course control).
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    DualCursorControl3.vi ‏64 KB

  • Algorithm question, please help

    Hi all,
    Suppose that we have several String lines (postal addresses for example) in a text file.
    Is there an algoritm that permits to find the list of the addresses that 'resembles' to
    the query (an address that may contain an error) entered by the user ?
    Any idea about the algorithms used in the commercial tools to do this ?
    thanks a lot,

    http://www.cs.sunysb.edu/~algorith/files/approximate-pattern-matching.shtml
    http://www.pnylab.com/pny/papers/string_compare/string_compare/index.html
    http://archive.nlm.nih.gov/pubs/hauser/Tompaper/tompaper.php
    http://www.everything2.com/index.pl?node=edition%20distance
    http://www.everything2.com/index.pl?node=search%20for%20similar%20sub-stringsJust to give you an idea. You can also look for the soundex algorithm, it might be sufficient and it's fairly easy to implement.

  • Graphs/charts question

    Is there a way to make the classic graph or chart completely one colour, i.e. remove any embossed effects? If possible i would like to have been able to remove or paint black the lines shown in my attached png.  I’ve tried painting them but cant get them to vanish!
    Many thanks
    Attachments:
    Untitled.png ‏24 KB

    Here is a good resource, look at the video on recoloring graphs:
    http://https://decibel.ni.com/content/groups/ui/blog/2010/04/29/creating-quality-uis-with-ni-labview...
    "There is a God shaped vacuum in the heart of every man which cannot be filled by any created thing, but only by God, the Creator, made known through Jesus." - Blaise Pascal

  • Algorithm question

    Hello all,
    if we have two different sql files then how to compare two sql algorithm?
    thanks

    If you have two files, then you can use UTL_FILE package.
    Here is a link to documentation:
    http://download-uk.oracle.com/docs/cd/B14117_01/appdev.101/b10802/u_file.htm#996728
    Peter D.

  • 10g graph MarkerText question

    I use a bar graph and display marker text above each bar. My problem is when the # shown above the bar is 3 or more digits the number runs into the other bars beside it and makes it tough to read. I want to be able to display the text above the bars vertically if at all possible. I tried the following:
    <MarkerText visible="true" markerTextAngleDefault="90"/>
    But that didn't change how the text is shown at all. Can anybody help me?
    Here's my full xml for the graph:
    <rw:graph id="CT_5" src="G_REGION" groups="REGION" dataValues="CS_REGION_WITH,CS_REGION_WITHOUT,CS_region_date_range">
    <?xml version="1.0" ?>
    <Graph version="3.2.0.22">
    <LegendArea automaticPlacement="AP_NEVER" position="LAP_BOTTOM" borderTransparent="true"/>
    <LegendText>
    <GraphFont size="16"/>
    </LegendText>
    <MarkerText visible="true" markerTextAngleDefault="90"/>
    <O1TickLabel>
    <GraphFont size="14"/>
    </O1TickLabel>
    <O1Title text="Region" visible="true">
    <GraphFont size="16"/>
    </O1Title>
    <Y1TickLabel>
    <GraphFont size="14"/>
    </Y1TickLabel>
    <Y1Title text="Number of Facilities" visible="true">
    <GraphFont size="16"/>
    </Y1Title>
    </Graph>
    </rw:graph>

    Ok, I just figured out you have to set markerTextPlace="MTP_CUSTOM" in <MarkerText> but even then I can't get the text to be displayed vertically. Please help.

  • Graph Algorithm Research Tool (GART)

    In reference to this thread:
    http://forum.java.sun.com/thread.jspa?forumID=426&threadID=435407
    around reply 20 I mentioned a Graph API but never gave a URL where
    it can be accessed, mainly because it wasn't ready.
    Well, it has just been released as part of a demo application called
    Gart Demo, available at:
    http://www.graphbuilder.com

    Java's Stack extends Vector, so you can insert objects in Java's stack in other positions, not just the top. The Stack in the com.graphbuilder.struc package extends LinkedList. Both stacks (com.graphbuilder.struc and java.util) don't add any additional functionality. The only advantage of extending linked list would be for cases where you need to add or remove something to the bottom without a penalty.
    I did a lot of testing on this in terms of speed. If you need speed, then using an array based stack is much faster. In fact, if you really need speed then you use your own array and maintain your own counter.
    i would like to see an API of the collection that follows the data structure definition.This is kind of like saying, "I only want 3 methods because that is what I was taught was a stack". I agree that the concept of a stack is typically 3 methods, but the implementation of a stack is built on top of either an array or linked list.

  • Faces algorithm question

    Hi,
    Could anyone tell me if the recognition algorithm will perform less accurately if "unclear" faces are added to a collection of proper faces for one given person? (for example faces which would be blurry, partially hidden, with a grimace, etc.)
    I am tempted to add all pictures for a given person, whatever the "quality" of the face on that picture. But I'm afraid of bringing confusion to the recognition algorithm...
    Thanks

    Don't worry, You can't confuse it more than it already is. My Land Rover is rare in that it does not have a name but it seems to Apple it does have a face. It hangs about on the rear door dressed up as a Spare Wheel. Allan

Maybe you are looking for