SlideShare a Scribd company logo
1 of 56
Data Structures and Algorithms
Objectives


                In this session, you will learn to:
                   Implement a graph
                   Apply graphs to solve programming problems




     Ver. 1.0                                                   Session 17
Data Structures and Algorithms
Representing a Graph


                To implement a graph, you need to first represent the given
                information in the form of a graph.
                The two most commonly used ways of representing a graph
                are as follows:
                   Adjacency Matrix
                   Adjacency List




     Ver. 1.0                                                       Session 17
Data Structures and Algorithms
Adjacency Matrix


                Consider the following   Adjacency Matrix Representation
                graph:
                                                     v1   v2   v3   v4

                                              v1     0    1    0     0
                                              v2     0    0    1     0
                                              v3     0    0    0     0
                                              v4     1    0    1     0




     Ver. 1.0                                                       Session 17
Data Structures and Algorithms
Adjacency List


                Consider the following   Adjacency List Representation
                graph:




     Ver. 1.0                                                     Session 17
Data Structures and Algorithms
Traversing a Graph


                Traversing a graph means visiting all the vertices in a
                graph.
                You can traverse a graph with the help of the following two
                methods:
                   Depth First Search (DFS)
                   Breadth First Search (BFS)




     Ver. 1.0                                                        Session 17
Data Structures and Algorithms
DFS


                 Algorithm: DFS(v)
                 1. Push the starting vertex, v into the stack.
                 2. Repeat until the stack becomes empty:
                     a. Pop a vertex from the stack.
                     b. Visit the popped vertex.
                     c. Push all the unvisited vertices adjacent to the popped vertex
                        into the stack.




      Ver. 1.0                                                                   Session 17
Data Structures and Algorithms
DFS (Contd.)


                Push the starting vertex, v1 into the stack




                                                              v1




     Ver. 1.0                                                      Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v1 from the stack
                Visit v1
                Push all unvisited vertices adjacent to v1 into the stack




                                                               v1




                             Visited:
                             v1
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v1 from the stack
                Visit v1
                Push all unvisited vertices adjacent to v1 into the stack




                                                               v2
                                                               v4




                             Visited:
                             v1
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v2 from the stack
                Visit v2
                Push all unvisited vertices adjacent to v2 into the stack




                                                               v2
                                                               v4




                             Visited:
                             v1 v2
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v2 from the stack
                Visit v2
                Push all unvisited vertices adjacent to v2 into the stack



                                                               v6
                                                               v3
                                                               v4




                             Visited:
                             v1 v2
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v6 from the stack
                Visit v6
                Push all unvisited vertices adjacent to v6 into the stack



                                                                v6
                                                                v3
                                                                v4




                                               There are no unvisited vertices
                                               adjacent to v6
                             Visited:
                             v1 v2 v6
     Ver. 1.0                                                            Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v3 from the stack
                Visit v3
                Push all unvisited vertices adjacent to v3 into the stack




                                                               v3
                                                               v4




                             Visited:
                             v1 v2 v6 v3
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v3 from the stack
                Visit v3
                Push all unvisited vertices adjacent to v3 into the stack




                                                               v5
                                                               v4




                             Visited:
                             v1 v2 v6 v3
     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v5 from the stack
                Visit v5
                Push all unvisited vertices adjacent to v5 into the stack




                                                                v5
                                                                v4




                                               There are no unvisited vertices
                                               adjacent to v5
                             Visited:
                             v1 v2 v6 v3 v5
     Ver. 1.0                                                            Session 17
Data Structures and Algorithms
DFS (Contd.)


                Pop a vertex, v4 from the stack
                Visit v4
                Push all unvisited vertices adjacent to v4 into the stack




                                                                v4




                                               There are no unvisited vertices
                                               adjacent to v4
                             Visited:
                             v1 v2 v6 v3 v5 v4
     Ver. 1.0                                                            Session 17
Data Structures and Algorithms
DFS (Contd.)


                The stack is now empty
                Therefore, traversal is complete




                            Visited:
                             v1 v2 v6 v3 v5 v4
     Ver. 1.0                                      Session 17
Data Structures and Algorithms
DFS (Contd.)


                β€’ Although the preceding algorithm provides a simple and
                  convenient method to traverse a graph, the algorithm will
                  not work correctly if the graph is not connected.
                β€’ In such a case, you will not be able to traverse all the
                  vertices from one single starting vertex.




     Ver. 1.0                                                          Session 17
Data Structures and Algorithms
DFS (Contd.)


                To solve this problem, you need to       1. Repeat step 2 for each
                                                            vertex, v in the graph
                execute the preceding algorithm
                                                         2. If v is not visited:
                repeatedly for all unvisited vertices in     a. Call DFS(v)
                the graph.




     Ver. 1.0                                                              Session 17
Data Structures and Algorithms
BFS


                 Algorithm: BFS(v)
                 1. Visit the starting vertex, v and insert it into a queue.
                 2. Repeat step 3 until the queue becomes empty.
                 3. Delete the front vertex from the queue, visit all its unvisited
                    adjacent vertices, and insert them into the queue.




      Ver. 1.0                                                                 Session 17
Data Structures and Algorithms
BFS (Contd.)


                Visit v1
                Insert v1 into the queue




                                           v1




                             v1
     Ver. 1.0                                   Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v1 from the queue
                β€’ Visit all unvisited vertices adjacent to v1 and insert them in
                  the queue



                                                     v1




                                Visited:
                                v1
     Ver. 1.0                                                             Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v1 from the queue
                β€’ Visit all unvisited vertices adjacent to v1 and insert them in
                  the queue




                                                     v2   v4




                                v1 v2 v4
     Ver. 1.0                                                             Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v2 from the queue
                β€’ Visit all unvisited vertices adjacent to v2 and insert them in
                  the queue




                                                     v2   v4




                                v1 v2 v4
     Ver. 1.0                                                             Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v2 from the queue
                β€’ Visit all unvisited vertices adjacent to v2 and insert them in
                  the queue




                                                          v4   v3   v6




                                v1 v2 v4 v3       v6
     Ver. 1.0                                                             Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v4 from the queue
                β€’ Visit all unvisited vertices adjacent to v4 and insert them in
                  the queue




                                                          v4   v3   v6   v5




                                v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                                 Session 17
Data Structures and Algorithms
BFS (Contd.)


                Remove a vertex v3 from the queue
                Visit all unvisited vertices adjacent to v3 and insert them in
                the queue




                                                            v3   v6   v5



                                                   v3 does not have any
                                                   unvisited adjacent vertices




                             v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                              Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v6 from the queue
                β€’ Visit all unvisited vertices adjacent to v6 and insert them in
                  the queue




                                                                   v6   v5



                                                      v3 does not have any
                                                      unvisited adjacent vertices




                                v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                                Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v6 from the queue
                β€’ Visit all unvisited vertices adjacent to v6 and insert them in
                  the queue




                                                                        v5



                                                      v6 does not have any
                                                      unvisited adjacent vertices




                                v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                                Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v5 from the queue
                β€’ Visit all unvisited vertices adjacent to v5 and insert them in
                  the queue




                                                                        v5



                                                      v6 does not have any
                                                      unvisited adjacent vertices




                                v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                                Session 17
Data Structures and Algorithms
BFS (Contd.)


                β€’ Remove a vertex v5 from the queue
                β€’ Visit all unvisited vertices adjacent to v5 and insert them in
                  the queue




                                                      v5 does not have any
                                                      unvisited adjacent vertices




                                v1 v2 v4 v3       v6 v5
     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
BFS (Contd.)


                The queue is now empty
                Therefore, traversal is complete




                                                   v5 does not have any
                                                   unvisited adjacent vertices




                             v1 v2 v4 v3      v6 v5
     Ver. 1.0                                                            Session 17
Data Structures and Algorithms
BFS (Contd.)


                Although the preceding algorithm provides a simple and
                convenient method to traverse a graph, the algorithm will
                not work correctly if the graph is not connected.
                In such a case, you will not be able to traverse all the
                vertices from one single starting vertex.




     Ver. 1.0                                                        Session 17
Data Structures and Algorithms
BFS (Contd.)


                To solve this problem, you need to       1. Repeat step 2 for each
                                                            vertex, v in the graph
                execute the preceding algorithm
                                                         2. If v is not visited:
                repeatedly for all unvisited vertices in     a. Call BFS(v)
                the graph.




     Ver. 1.0                                                             Session 17
Data Structures and Algorithms
Activity: Implementing a Graph by Using Adjacency Matrix Representation



                 Problem Statement:
                    You have to represent a set of cities and the distances
                    between them in the form of a graph. Write a program to
                    represent the graph in the form of an adjacency matrix.




      Ver. 1.0                                                            Session 17
Data Structures and Algorithms
Applications of Graphs


                Many problems can be easily solved by reducing them in
                the form of a graph
                Graph theory has been instrumental in analyzing and
                solving problems in areas as diverse as computer network
                design, urban planning, finding shortest paths and
                molecular biology.




     Ver. 1.0                                                      Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem


                The shortest path problem can be solved by applying the
                Dijkstra’s algorithm on a graph
                The Dijkstra’s algorithm is based on the greedy approach
                The steps in the Dijkstra’s algorithm are as follows:
                1. Choose vertex v corresponding to the smallest distance
                   recorded in the DISTANCE array such that v is not already in
                   FINAL.
                2. Add v to FINAL.
                3. Repeat for each vertex w in the graph that is not in FINAL:
                    a. If the path from v1 to w via v is shorter than the previously
                       recorded distance from v1 to w (If ((DISTANCE[v] + weight of
                       edge(v,w)) < DISTANCE[w])):
                         i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w).
                4. If FINAL does not contain all the vertices, go to step 1.


     Ver. 1.0                                                                    Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5                            Suppose you need to find the
                                                     shortest distance of all the
                                                     vertices from vertex v1.
                3           4               6
                                                     Add v1 to the FINAL array.

                        2
                                        3

                    6       3




                                                v1      v2   v3   v4 v5     v6
                                DISTANCE        0       5    ∞    3  ∞      ∞

                                FINAL           v1


     Ver. 1.0                                                                Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5                            In the DISTANCE array, vertex
                                                     v4 has the shortest distance
                                                     from vertex v1.
                3           4               6        Therefore, v4 is added to the
                                                     FINAL array.
                        2
                                        3

                    6       3




                                                v1      v2   v3    v4 v5     v6
                                DISTANCE        0        5   ∞     3  ∞      ∞

                                FINAL           v1     v4


     Ver. 1.0                                                                 Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v2 = 5
                                                     v1 β†’ v4 β†’ v2 = 3 + ∞ = ∞

                3           4               6
                                                             ∞>5
                                                     Therefore, no change is
                        2
                                        3            made.
                    6       3




                                                v1     v2   v3     v4 v5   v6
                                DISTANCE        0       5   ∞      3  ∞    ∞

                                FINAL           v1    v4


     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v3 = ∞
                                                     v1 β†’ v4 β†’ v3 = 3 + 2 = 5

                3           4               6
                                                             5<∞
                                                     Therefore, the entry
                        2
                                        3            corresponding to v3 in the
                                                     DISTANCE array is changed
                    6       3
                                                     to 5.

                                                v1     v2   v3     v4 v5   v6
                                DISTANCE        0       5   ∞
                                                            5      3  ∞    ∞

                                FINAL           v1    v4


     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v5 = ∞
                                                     v1 β†’ v4 β†’ v5 = 3 + 6 = 9

                3           4               6
                                                             9<∞
                                                     Therefore, the entry
                        2
                                        3            corresponding to v5 in the
                                                     DISTANCE array is changed
                    6       3
                                                     to 9.

                                                v1     v2   v3     v4 v5   v6
                                DISTANCE        0       5   5      3  ∞
                                                                      9    ∞

                                FINAL           v1    v4


     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v6 = ∞
                                                     v1 β†’ v4 β†’ v6 = 3 + ∞ = ∞

                3           4               6

                                                     Both the values are equal.
                        2
                                        3            Therefore, no change is made.
                    6       3
                                                     PASS 1 complete


                                                v1      v2   v3    v4 v5   v6
                                DISTANCE        0       5    5     3  9    ∞

                                FINAL           v1     v4


     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     From the DISTANCE array,
                                                     select the vertex with the
                                                     shortest distance from v1, such
                                                     that the selected vertex is not
                3           4               6
                                                     in the FINAL array.

                        2
                                                     v2 and v3 have the shortest
                                        3            and the same distance from v1.
                    6       3                        Let us select v2 and add it to
                                                     the FINAL array.

                                                v1      v2    v3   v4 v5      v6
                                DISTANCE        0        5    5    3  9       ∞

                                FINAL           v1     v4    v2

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v3 = 5
                                                     v1 β†’ v2 β†’ v3 = 5 + 4 = 9

                3           4               6
                                                             9>5
                                                     Therefore, no change is
                        2
                                        3            made.
                    6       3




                                                v1     v2   v3     v4 v5   v6
                                DISTANCE        0       5   5      3  9    ∞

                                FINAL           v1    v4    v2

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v5 = 9
                                                     v1 β†’ v2 β†’ v5 = 5 + ∞ = ∞

                3           4               6
                                                             ∞>9
                                                     Therefore, no change is
                        2
                                        3            made.
                    6       3




                                                v1     v2   v3     v4 v5   v6
                                DISTANCE        0       5   5      3  9    ∞

                                FINAL           v1    v4    v2

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v6 = ∞
                                                     v1 β†’ v2 β†’ v6 = 5 + 6 = 11

                3           4               6
                                                             11 < ∞
                                                     Therefore, the entry
                        2
                                        3            corresponding to v6 in the
                                                     DISTANCE array is changed
                    6       3
                                                     to 11.
                                                              Pass 2 complete

                                                v1     v2   v3     v4 v5 v6
                                DISTANCE        0       5   5      3  9   ∞
                                                                         11

                                FINAL           v1    v4    v2

     Ver. 1.0                                                              Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     From the DISTANCE array,
                                                     select the vertex with the
                                                     shortest distance from v1, such
                                                     that the selected vertex is not
                3           4               6
                                                     in the FINAL array.

                        2                            Let us select v3 and add it to
                                        3
                                                     the FINAL array.
                    6       3




                                                v1      v2    v3   v4 v5 v6
                                DISTANCE        0        5    5    3  9  11

                                FINAL           v1     v4    v2    v3

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v5 = 9
                                                     v1 β†’ v3 β†’ v5 = 5 + 3 = 8

                3           4               6
                                                             8<9
                                                     Therefore, the entry
                        2
                                        3            corresponding to v5 in the
                                                     DISTANCE array is changed
                    6       3
                                                     to 8.

                                                v1     v2   v3     v4 v5 v6
                                DISTANCE        0       5   5      3  9
                                                                      8  11

                                FINAL           v1    v4    v2     v3

     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v6 = 11
                                                     v1 β†’ v3 β†’ v6 = 5 + 3 = 8

                3           4               6
                                                             8 < 11
                                                     Therefore, the entry
                        2
                                        3            corresponding to v6 in the
                                                     DISTANCE array is changed
                    6       3
                                                     to 8.
                                                               Pass 3 complete

                                                v1     v2   v3      v4 v5 v6
                                DISTANCE        0       5   5       3  8  11
                                                                           8

                                FINAL           v1    v4    v2   v3

     Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     From the DISTANCE array,
                                                     select the vertex with the
                                                     shortest distance from v1, such
                                                     that the selected vertex is not
                3           4               6
                                                     in the FINAL array.

                        2                            Let us select v5 and add it to
                                        3
                                                     the FINAL array.
                    6       3




                                                v1      v2    v3   v4 v5      v6
                                DISTANCE        0        5    5    3  8       8

                                FINAL           v1     v4    v2    v3   v5

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     v1 β†’ v6 = 8
                                                     v1 β†’ v5 β†’ v6 = 8 + ∞ = ∞

                3           4               6
                                                             ∞>8
                                                     Therefore, no change is
                        2
                                        3            made.
                    6       3
                                                                 Pass 4 complete

                                                v1     v2   v3     v4 v5     v6
                                DISTANCE        0       5   5      3  8      8

                                FINAL           v1    v4    v2     v3   v5

     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Solving the Shortest Path Problem (Contd.)

                        5
                                                     Now add the only remaining
                                                     vertex, v6 to the FINAL
                                                     array.
                3           4               6        All vertices have been
                                                     added to the FINAL array.
                        2
                                                     This means that the
                                        3
                                                     DISTANCE array now
                    6       3
                                                     contains the shortest
                                                     distances from vertex v1 to
                                                     all other vertices.
                                                v1   v2    v3   v4 v5      v6
                                DISTANCE        0     5    5    3  8       8

                                FINAL           v1   v4   v2    v3   v5   v6


     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Activity: Solving the Shortest Path Problem


                Problem Statement:
                   In the previous activity, you created a program to represent a
                   set of cities and the distances between them in the form of a
                   graph. Extend the program to include the functionality for
                   finding the shortest path from a given city to all the other cities.




     Ver. 1.0                                                                  Session 17
Data Structures and Algorithms
Summary


               In this session, you learned that:
                  The two most commonly used ways of representing a graph
                  are as follows:
                    – Adjacency matrix
                    – Adjacency list
                  Traversing a graph means visiting all the vertices in the graph.
                  In a graph, there is no special vertex designated as the starting
                  vertex. Therefore, traversal of the graph may start from any
                  vertex.
                  You can traverse a graph with the help of the following two
                  methods:
                      DFS
                      BFS




    Ver. 1.0                                                               Session 17
Data Structures and Algorithms
Summary (Contd.)


               Graph theory has been instrumental in analyzing and solving
               problems in areas as diverse as computer network design,
               urban planning, finding shortest paths and molecular biology.




    Ver. 1.0                                                          Session 17

More Related Content

More from Niit Care

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 bNiit Care
Β 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 bNiit Care
Β 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 aNiit Care
Β 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 cNiit Care
Β 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 bNiit Care
Β 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 aNiit Care
Β 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 cNiit Care
Β 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 bNiit Care
Β 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 aNiit Care
Β 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 cNiit Care
Β 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 aNiit Care
Β 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 cNiit Care
Β 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-cNiit Care
Β 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-bNiit Care
Β 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-aNiit Care
Β 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-cNiit Care
Β 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-bNiit Care
Β 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-aNiit Care
Β 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 bNiit Care
Β 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 cNiit Care
Β 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
Β 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
Β 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
Β 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
Β 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
Β 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
Β 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
Β 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
Β 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
Β 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
Β 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
Β 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
Β 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
Β 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
Β 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
Β 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
Β 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
Β 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
Β 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
Β 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
Β 

Recently uploaded

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
Β 
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
Β 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
Β 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
Β 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
Β 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
Β 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
Β 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
Β 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
Β 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
Β 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
Β 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Β 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
Β 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
Β 

Recently uploaded (20)

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Β 
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
Β 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Β 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
Β 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Β 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
Β 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
Β 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Β 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
Β 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Β 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Β 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Β 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
Β 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
Β 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Β 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Β 

12 ds and algorithm session_17

  • 1. Data Structures and Algorithms Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems Ver. 1.0 Session 17
  • 2. Data Structures and Algorithms Representing a Graph To implement a graph, you need to first represent the given information in the form of a graph. The two most commonly used ways of representing a graph are as follows: Adjacency Matrix Adjacency List Ver. 1.0 Session 17
  • 3. Data Structures and Algorithms Adjacency Matrix Consider the following Adjacency Matrix Representation graph: v1 v2 v3 v4 v1 0 1 0 0 v2 0 0 1 0 v3 0 0 0 0 v4 1 0 1 0 Ver. 1.0 Session 17
  • 4. Data Structures and Algorithms Adjacency List Consider the following Adjacency List Representation graph: Ver. 1.0 Session 17
  • 5. Data Structures and Algorithms Traversing a Graph Traversing a graph means visiting all the vertices in a graph. You can traverse a graph with the help of the following two methods: Depth First Search (DFS) Breadth First Search (BFS) Ver. 1.0 Session 17
  • 6. Data Structures and Algorithms DFS Algorithm: DFS(v) 1. Push the starting vertex, v into the stack. 2. Repeat until the stack becomes empty: a. Pop a vertex from the stack. b. Visit the popped vertex. c. Push all the unvisited vertices adjacent to the popped vertex into the stack. Ver. 1.0 Session 17
  • 7. Data Structures and Algorithms DFS (Contd.) Push the starting vertex, v1 into the stack v1 Ver. 1.0 Session 17
  • 8. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack v1 Visited: v1 Ver. 1.0 Session 17
  • 9. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack v2 v4 Visited: v1 Ver. 1.0 Session 17
  • 10. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack v2 v4 Visited: v1 v2 Ver. 1.0 Session 17
  • 11. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack v6 v3 v4 Visited: v1 v2 Ver. 1.0 Session 17
  • 12. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v6 from the stack Visit v6 Push all unvisited vertices adjacent to v6 into the stack v6 v3 v4 There are no unvisited vertices adjacent to v6 Visited: v1 v2 v6 Ver. 1.0 Session 17
  • 13. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack v3 v4 Visited: v1 v2 v6 v3 Ver. 1.0 Session 17
  • 14. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack v5 v4 Visited: v1 v2 v6 v3 Ver. 1.0 Session 17
  • 15. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v5 from the stack Visit v5 Push all unvisited vertices adjacent to v5 into the stack v5 v4 There are no unvisited vertices adjacent to v5 Visited: v1 v2 v6 v3 v5 Ver. 1.0 Session 17
  • 16. Data Structures and Algorithms DFS (Contd.) Pop a vertex, v4 from the stack Visit v4 Push all unvisited vertices adjacent to v4 into the stack v4 There are no unvisited vertices adjacent to v4 Visited: v1 v2 v6 v3 v5 v4 Ver. 1.0 Session 17
  • 17. Data Structures and Algorithms DFS (Contd.) The stack is now empty Therefore, traversal is complete Visited: v1 v2 v6 v3 v5 v4 Ver. 1.0 Session 17
  • 18. Data Structures and Algorithms DFS (Contd.) β€’ Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. β€’ In such a case, you will not be able to traverse all the vertices from one single starting vertex. Ver. 1.0 Session 17
  • 19. Data Structures and Algorithms DFS (Contd.) To solve this problem, you need to 1. Repeat step 2 for each vertex, v in the graph execute the preceding algorithm 2. If v is not visited: repeatedly for all unvisited vertices in a. Call DFS(v) the graph. Ver. 1.0 Session 17
  • 20. Data Structures and Algorithms BFS Algorithm: BFS(v) 1. Visit the starting vertex, v and insert it into a queue. 2. Repeat step 3 until the queue becomes empty. 3. Delete the front vertex from the queue, visit all its unvisited adjacent vertices, and insert them into the queue. Ver. 1.0 Session 17
  • 21. Data Structures and Algorithms BFS (Contd.) Visit v1 Insert v1 into the queue v1 v1 Ver. 1.0 Session 17
  • 22. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v1 from the queue β€’ Visit all unvisited vertices adjacent to v1 and insert them in the queue v1 Visited: v1 Ver. 1.0 Session 17
  • 23. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v1 from the queue β€’ Visit all unvisited vertices adjacent to v1 and insert them in the queue v2 v4 v1 v2 v4 Ver. 1.0 Session 17
  • 24. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v2 from the queue β€’ Visit all unvisited vertices adjacent to v2 and insert them in the queue v2 v4 v1 v2 v4 Ver. 1.0 Session 17
  • 25. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v2 from the queue β€’ Visit all unvisited vertices adjacent to v2 and insert them in the queue v4 v3 v6 v1 v2 v4 v3 v6 Ver. 1.0 Session 17
  • 26. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v4 from the queue β€’ Visit all unvisited vertices adjacent to v4 and insert them in the queue v4 v3 v6 v5 v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 27. Data Structures and Algorithms BFS (Contd.) Remove a vertex v3 from the queue Visit all unvisited vertices adjacent to v3 and insert them in the queue v3 v6 v5 v3 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 28. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v6 from the queue β€’ Visit all unvisited vertices adjacent to v6 and insert them in the queue v6 v5 v3 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 29. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v6 from the queue β€’ Visit all unvisited vertices adjacent to v6 and insert them in the queue v5 v6 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 30. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v5 from the queue β€’ Visit all unvisited vertices adjacent to v5 and insert them in the queue v5 v6 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 31. Data Structures and Algorithms BFS (Contd.) β€’ Remove a vertex v5 from the queue β€’ Visit all unvisited vertices adjacent to v5 and insert them in the queue v5 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 32. Data Structures and Algorithms BFS (Contd.) The queue is now empty Therefore, traversal is complete v5 does not have any unvisited adjacent vertices v1 v2 v4 v3 v6 v5 Ver. 1.0 Session 17
  • 33. Data Structures and Algorithms BFS (Contd.) Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. In such a case, you will not be able to traverse all the vertices from one single starting vertex. Ver. 1.0 Session 17
  • 34. Data Structures and Algorithms BFS (Contd.) To solve this problem, you need to 1. Repeat step 2 for each vertex, v in the graph execute the preceding algorithm 2. If v is not visited: repeatedly for all unvisited vertices in a. Call BFS(v) the graph. Ver. 1.0 Session 17
  • 35. Data Structures and Algorithms Activity: Implementing a Graph by Using Adjacency Matrix Representation Problem Statement: You have to represent a set of cities and the distances between them in the form of a graph. Write a program to represent the graph in the form of an adjacency matrix. Ver. 1.0 Session 17
  • 36. Data Structures and Algorithms Applications of Graphs Many problems can be easily solved by reducing them in the form of a graph Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology. Ver. 1.0 Session 17
  • 37. Data Structures and Algorithms Solving the Shortest Path Problem The shortest path problem can be solved by applying the Dijkstra’s algorithm on a graph The Dijkstra’s algorithm is based on the greedy approach The steps in the Dijkstra’s algorithm are as follows: 1. Choose vertex v corresponding to the smallest distance recorded in the DISTANCE array such that v is not already in FINAL. 2. Add v to FINAL. 3. Repeat for each vertex w in the graph that is not in FINAL: a. If the path from v1 to w via v is shorter than the previously recorded distance from v1 to w (If ((DISTANCE[v] + weight of edge(v,w)) < DISTANCE[w])): i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w). 4. If FINAL does not contain all the vertices, go to step 1. Ver. 1.0 Session 17
  • 38. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 Suppose you need to find the shortest distance of all the vertices from vertex v1. 3 4 6 Add v1 to the FINAL array. 2 3 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 ∞ 3 ∞ ∞ FINAL v1 Ver. 1.0 Session 17
  • 39. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 In the DISTANCE array, vertex v4 has the shortest distance from vertex v1. 3 4 6 Therefore, v4 is added to the FINAL array. 2 3 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 ∞ 3 ∞ ∞ FINAL v1 v4 Ver. 1.0 Session 17
  • 40. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v2 = 5 v1 β†’ v4 β†’ v2 = 3 + ∞ = ∞ 3 4 6 ∞>5 Therefore, no change is 2 3 made. 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 ∞ 3 ∞ ∞ FINAL v1 v4 Ver. 1.0 Session 17
  • 41. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v3 = ∞ v1 β†’ v4 β†’ v3 = 3 + 2 = 5 3 4 6 5<∞ Therefore, the entry 2 3 corresponding to v3 in the DISTANCE array is changed 6 3 to 5. v1 v2 v3 v4 v5 v6 DISTANCE 0 5 ∞ 5 3 ∞ ∞ FINAL v1 v4 Ver. 1.0 Session 17
  • 42. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v5 = ∞ v1 β†’ v4 β†’ v5 = 3 + 6 = 9 3 4 6 9<∞ Therefore, the entry 2 3 corresponding to v5 in the DISTANCE array is changed 6 3 to 9. v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 ∞ 9 ∞ FINAL v1 v4 Ver. 1.0 Session 17
  • 43. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v6 = ∞ v1 β†’ v4 β†’ v6 = 3 + ∞ = ∞ 3 4 6 Both the values are equal. 2 3 Therefore, no change is made. 6 3 PASS 1 complete v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 ∞ FINAL v1 v4 Ver. 1.0 Session 17
  • 44. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not 3 4 6 in the FINAL array. 2 v2 and v3 have the shortest 3 and the same distance from v1. 6 3 Let us select v2 and add it to the FINAL array. v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 ∞ FINAL v1 v4 v2 Ver. 1.0 Session 17
  • 45. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v3 = 5 v1 β†’ v2 β†’ v3 = 5 + 4 = 9 3 4 6 9>5 Therefore, no change is 2 3 made. 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 ∞ FINAL v1 v4 v2 Ver. 1.0 Session 17
  • 46. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v5 = 9 v1 β†’ v2 β†’ v5 = 5 + ∞ = ∞ 3 4 6 ∞>9 Therefore, no change is 2 3 made. 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 ∞ FINAL v1 v4 v2 Ver. 1.0 Session 17
  • 47. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v6 = ∞ v1 β†’ v2 β†’ v6 = 5 + 6 = 11 3 4 6 11 < ∞ Therefore, the entry 2 3 corresponding to v6 in the DISTANCE array is changed 6 3 to 11. Pass 2 complete v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 ∞ 11 FINAL v1 v4 v2 Ver. 1.0 Session 17
  • 48. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not 3 4 6 in the FINAL array. 2 Let us select v3 and add it to 3 the FINAL array. 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 11 FINAL v1 v4 v2 v3 Ver. 1.0 Session 17
  • 49. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v5 = 9 v1 β†’ v3 β†’ v5 = 5 + 3 = 8 3 4 6 8<9 Therefore, the entry 2 3 corresponding to v5 in the DISTANCE array is changed 6 3 to 8. v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 9 8 11 FINAL v1 v4 v2 v3 Ver. 1.0 Session 17
  • 50. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v6 = 11 v1 β†’ v3 β†’ v6 = 5 + 3 = 8 3 4 6 8 < 11 Therefore, the entry 2 3 corresponding to v6 in the DISTANCE array is changed 6 3 to 8. Pass 3 complete v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 8 11 8 FINAL v1 v4 v2 v3 Ver. 1.0 Session 17
  • 51. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not 3 4 6 in the FINAL array. 2 Let us select v5 and add it to 3 the FINAL array. 6 3 v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 8 8 FINAL v1 v4 v2 v3 v5 Ver. 1.0 Session 17
  • 52. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 v1 β†’ v6 = 8 v1 β†’ v5 β†’ v6 = 8 + ∞ = ∞ 3 4 6 ∞>8 Therefore, no change is 2 3 made. 6 3 Pass 4 complete v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 8 8 FINAL v1 v4 v2 v3 v5 Ver. 1.0 Session 17
  • 53. Data Structures and Algorithms Solving the Shortest Path Problem (Contd.) 5 Now add the only remaining vertex, v6 to the FINAL array. 3 4 6 All vertices have been added to the FINAL array. 2 This means that the 3 DISTANCE array now 6 3 contains the shortest distances from vertex v1 to all other vertices. v1 v2 v3 v4 v5 v6 DISTANCE 0 5 5 3 8 8 FINAL v1 v4 v2 v3 v5 v6 Ver. 1.0 Session 17
  • 54. Data Structures and Algorithms Activity: Solving the Shortest Path Problem Problem Statement: In the previous activity, you created a program to represent a set of cities and the distances between them in the form of a graph. Extend the program to include the functionality for finding the shortest path from a given city to all the other cities. Ver. 1.0 Session 17
  • 55. Data Structures and Algorithms Summary In this session, you learned that: The two most commonly used ways of representing a graph are as follows: – Adjacency matrix – Adjacency list Traversing a graph means visiting all the vertices in the graph. In a graph, there is no special vertex designated as the starting vertex. Therefore, traversal of the graph may start from any vertex. You can traverse a graph with the help of the following two methods: DFS BFS Ver. 1.0 Session 17
  • 56. Data Structures and Algorithms Summary (Contd.) Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology. Ver. 1.0 Session 17

Editor's Notes

  1. Ask students to answer this question and then come to the given example.
  2. Ask students to answer this question and then come to the given example.
  3. Ask students to answer this question and then come to the given example.
  4. Ask students to answer this question and then come to the given example.
  5. Ask students to answer this question and then come to the given example.
  6. Ask students to answer this question and then come to the given example.
  7. Ask students to answer this question and then come to the given example.
  8. Ask students to answer this question and then come to the given example.
  9. Ask students to answer this question and then come to the given example.
  10. Ask students to answer this question and then come to the given example.
  11. Ask students to answer this question and then come to the given example.
  12. Ask students to answer this question and then come to the given example.
  13. Ask students to answer this question and then come to the given example.
  14. Ask students to answer this question and then come to the given example.
  15. Ask students to answer this question and then come to the given example.
  16. Ask students to answer this question and then come to the given example.
  17. Ask students to answer this question and then come to the given example.
  18. Ask students to answer this question and then come to the given example.
  19. Ask students to answer this question and then come to the given example.
  20. Ask students to answer this question and then come to the given example.
  21. Ask students to answer this question and then come to the given example.
  22. Ask students to answer this question and then come to the given example.
  23. Ask students to answer this question and then come to the given example.
  24. Ask students to answer this question and then come to the given example.
  25. Ask students to answer this question and then come to the given example.
  26. Ask students to answer this question and then come to the given example.
  27. Ask students to answer this question and then come to the given example.
  28. Ask students to answer this question and then come to the given example.
  29. Ask students to answer this question and then come to the given example.
  30. Ask students to answer this question and then come to the given example.
  31. Ask students to answer this question and then come to the given example.
  32. Ask students to answer this question and then come to the given example.
  33. Ask students to answer this question and then come to the given example.
  34. Ask students to answer this question and then come to the given example.
  35. Ask students to answer this question and then come to the given example.
  36. Ask students to answer this question and then come to the given example.
  37. Ask students to answer this question and then come to the given example.
  38. Ask students to answer this question and then come to the given example.
  39. Ask students to answer this question and then come to the given example.
  40. Ask students to answer this question and then come to the given example.
  41. Ask students to answer this question and then come to the given example.
  42. Ask students to answer this question and then come to the given example.
  43. Ask students to answer this question and then come to the given example.
  44. Ask students to answer this question and then come to the given example.
  45. Ask students to answer this question and then come to the given example.
  46. Ask students to answer this question and then come to the given example.
  47. Ask students to answer this question and then come to the given example.
  48. Ask students to answer this question and then come to the given example.
  49. Ask students to answer this question and then come to the given example.