Docstoc

ADT Graph

Document Sample
ADT Graph Powered By Docstoc
					                          ADT Graph
A graph is a finite set of points (called nodes or vertices), some of
  which are connected by lines (called edges)
For Example,

                                  A


                                                                 B


               C


    D                                             F

                   E

                                 G
         H
                                                             I
                              ADT Graph
Set of vertices - V = {A, B, C, D, E, F, G, H, I}




                                        A


                                                            B


                C


    D                                               F

                      E

                                       G
          H
                                                        I
                                ADT Graph
If an edge connects (i.e. line between) vertices P and Q, then the edge can be represented
    by un-ordered pair (P, Q).
 (P, Q) : edge from P to Q or edge from Q to P
Set of edges for the graph below :
E = { (A,B), (A,E) , (A,F), (B,G), (B,F), (B,I), (C,D), (C,E), (C,F), (D, H), (F,G), (H,I) }
                                           A


                                                                                  B


               C


   D                                                           F

                       E

                                         G
         H
                                                                              I
                             Directed Graph
Edge from P to Q can be represented by ordered pair (P, Q).
Set of edges for the graph below :
E = { (A, E), (A, F), (B, A), (C, D), (C, F), (C, I), (E, C), (E, H),
       (F, B), (F, G), (G, B), (H, D), (I, B), (I, H) }

                                       A


                                                                  B


               C


    D                                                   F

                     E

                                     G
         H
                                                              I
                          Weighted Graph

Edges with weights – (positive) integer


                                          A
                                                      10
                                                  5
                                                                       B
                                3
                                              7                4
               C

                8               4                                      6
                                                           F

                      E

                                      G
                                                                   I
                 Computer representations of graphs

    Adjacency lists using link-list

                                          A:    B               E          F   null
                   A
                                          B:    A               F   null

                                      B
                                          C:    D               E          F   null

      C
                                          D:    C   null


D                           F
                                          E:    A               C   null

          E
                                           F:   A           B          C       G      null
                  G
                                           G:   F    null
          Computer representations of directed graphs

    Adjacency lists using link-list

                                          A:     B            F   null
                   A
                                          B:     F    null

                                      B
                                          C:     D            F   null

      C
                                          D:    empty list

D                           F
                                          E:     A            C   null

          E
                                           F:   empty list
                  G
                                           G:     F    null
    Computer representations of weighted directed graphs

    Adjacency lists using link-list

                                          A:     B 10                F 6     null
                   A
                            10
                                          B:     F    7      null

                                      B
               5        6                 C:     D 2                 F   3   null

      C            3             7
2                                         D:    empty list

D     4                     F
                                          E:     A 5                 C 4     null

          E
                       44                  F:   empty list
                   G
                                           G:     F    4      null
Efficient and elegant algorithms exist for solving a number of graph problems.

For example,
• What is the shortest path between two vertices?
• Is there a vertex whose removal disconnects graph?
• Can you draw the graph in the plane with no crossing edges?
• What is the largest edge set such that no edges in the set have a common end
   point?
• How to move through a graph so that can visit every node or every edge in a certain
   order?
• What is the best way to connect all of vertices using minimum cost?
• Etc.

These algorithms can be applied to solve real world problems.

Graphs are useful abstractions for numerous problems and structures in computer
   science, electrical engineering, communications, operations research, game theory
   and many other areas.
                      Graphs in real world

Graphs                 Vertices                         Edges

communication          telephones, computers            cables
circuits               gates, registers, processors     wires
transportation         street intersections, airports   highways,
                                                        flight routes
scheduling             tasks                            precedence
                                                        constraints
internet               web pages                        hyperlinks
games                  board positions                  legal moves
social relationship    people                           friendships

And many more …
                            Graph Traversals
Most algorithms for solving problems on a graph examine or process each vertex or
  edge.

A number of ways we can move through a graph so that can visit every node or
   every edge in a certain order.

Two traversals methods: Depth First Search(DFS) & Breadth First Search(BFS)

These methods provide particularly useful and efficient ways for solving problems.

For example,
Depth first search: order of the vertices visited and traversed-edges can be used for
   identifying articulation-points (whose removal will disconnects the graph)

Breadth first search: Search engines can traverse the internet (web-graph) using this
   method.
                                     Depth first search
Start at arbitrarily chosen (not visited) vertex.
When each new vertex is visited, a path is followed as far as possible, “visiting” or processing all
    the vertices along the way,
    until “dead end” is reached.
A dead end is a vertex such that all its neighbours have already been visited.
At a dead end we back up along the last edge traversed and branch out in another direction.

Order in which vertices visited : A B C H G I E D J F



                            v                                v
  v                        A                                B
 F                                       v                                            v
                                         G                                            C
                     v
                     I                                     H
                                                           v

                                                             v D                        J v
        E v
                                      Depth first search – using Stack

MyStack S = new myStack()
Choose a vertex (not visited) arbitrarily, x (say); Visit x; s.push(x)
While (! S.isEmpty( ) )
   {
         while there is a vertex, y, which is not visited and adjacent (neighbour) to S.peek( )
         {       visit y;
                  S.push(y)
         }
         S.pop( )
     }                                                                                  E
                 A            B
                                                              G                  I      I         I
                                                              H          H       H      H         H   H
 F                                                            C          C       C      C         C   C
                      G
                                      C
                                                              B          B       B      B         B   B
                                                              A          A       A      A         A   A
             I                H
                                                           J
                                                   D       D       D
                                  D    J
                                            C      C       C       C         C
     E                                      B      B       B       B         B   B                F
                                            A      A       A       A         A   A A              A   A
                                       Breadth first Search
Vertices are visited in order of increasing distance (# of edges) from the starting point.
Step 1:Start at arbitrarily chosen (not visited) vertex, x (say)
Step 2: Visit all the (not visited) vertices adjacent to x.
Step 3: Repeat Step 2 for each vertex (not visited) w adjacent to x.

Order in which vertices visited : A B F C G E I H D J

                                                                       v
                             v   A                                 B


v   F

                                              G
                                                                                             C
                                                                                                 v
                                              v

                         I                                      H

                             v                                 v
                                                                                                 v
                                                                    v      D                 J

            E   v
Breadth first Search – using Queue             myQueue Q = new myQueue ( )
                                             Start at arbitrarily chosen (not visited) vertex, x;
                                             visit (x); Q.enqueue(x)
                                             While ( ! Q.IsEmpty( ))
                                             { y = Q.front( _); Q.dequeue( _);
                 v                              for each vertex (not visited) w adjacent to y
                                    v                { visit (w);
                     A          B                         Q.Enqueue(w)
                                                       }
   v                                         }
       F                                     x=A
                                             Q:A
                                             y = A;               Q : empty
                         G v                 w = B;               Q: B
                                        vC   w = F;               Q : B, F
                                             y = B;               Q: F
                                             w = G;               Q : F, G
                                             w = C;               Q : F, G, C
                                             y = F;               Q: G, C
               v I             vH            w = E;               Q : G, C, E
                                              w= I;               Q : G, C, E, I
                                             y=G                  Q : C, E, I
                                             y=C                  Q: E, I
                                             w = H;               Q : E, I, H
                                             w = D;               Q : E, I, H, D
                                             y=E                  Q: I, H, D
                                    D    J   y=I                  Q: H, D
                                v            y=H                  Q: D
       v                                 v   y=D                  Q: empty
           E
                                             w=J                  Q: J
                                             y=J                  Q: empty
                                Graph Traversals

If two vertices are adjacent to vertex v (say), which will be visited first?
The answer will depend on implementation details, for example, the way in which the
    vertices are numbered or arranged in the representation (adjacency lists) of the
    graph.

Note that the preceding traversals methods (Depth First Search(DFS) & Breadth First
   Search(BFS) will visit only those vertices that can be reached by a path from the
   starting vertex (A).

If it is necessary to visit every vertex in the graph, instructions must be added to find a
     vertex which is not visited after the traversal from A is complete and to begin
     again from the new starting point.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:48
posted:10/5/2012
language:English
pages:16