Results 1 to 8 of 8

Thread: Diameter of a Graph

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Diameter of a Graph

    I need to write a function to compute the diameter of a graph given an array of edges, ie:

    1, 2
    1, 4
    2, 3
    3, 4

    Any ideas on how to go about doing this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Diameter of a Graph

    the diameter? the coordinates plot a 45, 45, 90 triangle.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Diameter of a Graph

    Quote Originally Posted by choey2k5
    the diameter? the coordinates plot a 45, 45, 90 triangle.
    There are four vertices, so it makes a square. I was spitting those off the top of my head, but regardless, the diameter would be 2.

    The diameter of a graph is the greatest distance between two vertices.

    Here's a better example. The diameter is 3:

    1, 3
    1, 6
    1, 8
    2, 5
    2, 6
    2, 7
    3, 4
    3, 8
    4, 5
    4, 9
    6, 7
    6, 8
    8, 9

    Any ideas on how to write a function to compute this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Junior Member
    Join Date
    Mar 2005
    Posts
    26

    Re: Diameter of a Graph

    just having the information that there are 4 coordinates does not mean that it'll plot a rectangle... there can be 50 thousand points that are colinear.

    i guess you made a mistake when you pulled them out of your head because (1,2), (2,3), and (3,4) makes a straight line. just being picky

    right off the head, i would have to guess that you should put the x's and y's in 2 arrays, and combinations of any 2 coordinates to find the distances, store them, and at the end find whatever value is the greatest, and go back to find out which 2 coordinates will give that value.

    i'll think about it some more...

    EDIT:

    hmm... how is the diameter for those plots is 3? the slope is 3 and the distance between the farthest 2 points [ (1,3) and (8,9) ] is Sqrt(85), or about 9.2195. i don't think i understood this diameter thing right...

    EDIT:

    seems more complicated than i imagined
    this might help: http://www.math.carleton.ca/~daniel/...y/allpairs.txt
    Last edited by choey2k5; Mar 16th, 2005 at 11:40 PM.

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Diameter of a Graph

    I don't think I've explained this very well.

    In my last example, 1-9 are all vertices within a graph. 1, 3 means that there is an edge between vertex 1 and 3. I think you're thinking more of traditional graph.

    Sorry if I'm not explaining this very well.

    So, for example, the distance between 1 and 3 is 1, because there's an edge between 1 and 3. The distance between 1 and 4 is 2, because the shortest way to get from 1 to 4 is 1 to 3, then 3 to 4.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Diameter of a Graph

    Quote Originally Posted by The Hobo
    I don't think I've explained this very well.

    In my last example, 1-9 are all vertices within a graph. 1, 3 means that there is an edge between vertex 1 and 3. I think you're thinking more of traditional graph.

    Sorry if I'm not explaining this very well.

    So, for example, the distance between 1 and 3 is 1, because there's an edge between 1 and 3. The distance between 1 and 4 is 2, because the shortest way to get from 1 to 4 is 1 to 3, then 3 to 4.
    Draw a rough picture in MSPaint, save it as a gif and post it up here.
    I don't live here any more.

  7. #7
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Diameter of a Graph

    Very difficult to do.

    This is because although you express the edges and vertices as a visible picture it doesn't necessarily have to be the picture you've drawn.

    Any graph can normally be drawn in many numbers of ways. Which means you can only find the diameter for a given graph drawing instance and not the graph itself.

    What you will need to do is store the vertex coordinates alongside the graph. Once you've done this you can then find the outermost vertices and hence the outermost edges. Once you have a circular walk of the outermost vertices/edges you have the diameter (by summing the length of each edge on the walk)

    The best way, I reckon of achieveing this is for a [i,j] matrix store k layers. The incidence matrix should be k=0, and k=1 could be x coordinates, k=2 could be y coordinates, and so on until k=n

    Once you have this you figure out the circle that includes all of the vertices. You can then use the centre of that circle to figure out which vertices are the furthest away from the centre.

    Then you go for a walk as they say . . .

    (It may be easier to figure out the centre and use vector algebra to calculate distances)
    Last edited by yrwyddfa; Mar 17th, 2005 at 08:25 AM.

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Diameter of a Graph

    Oh yeah, I forgot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width