Page 37 - Algorithms Notes for Professionals
P. 37

for (int i = 1; i <= v; i++)
                   {
                       System.out.print(i + " ");
                       for (int j = 1; j <= v; j++)
                           System.out.print(graph.getEdge(i, j) + " ");
                       System.out.println();
                   }

               }
               catch (Exception E)
               {
                   System.out.println("Somthing went wrong");
               }

               sc.close();
           }
       }


       Running the code: Save the file and compile using javac Represent_Graph_Adjacency_Matrix.java
       Example:



       $ java Represent_Graph_Adjacency_Matrix
       Enter the number of vertices:
       4
       Enter the number of edges:
       6
       Enter the edges:
       1 1
       3 4
       2 3
       1 4
       2 4
       1 2
       The adjacency matrix for the given graph is:
       1 2 3 4
       1 1 1 0 1
       2 0 0 1 1
       3 0 0 0 1
       4 0 0 0 0



       Section 9.2: Introduction To Graph Theory


       Graph Theory is the study of graphs, which are mathematical structures used to model pairwise relations between
       objects.

       Did you know, almost all the problems of planet Earth can be converted into problems of Roads and Cities, and
       solved? Graph Theory was invented many years ago, even before the invention of computer. Leonhard Euler wrote
       a paper on the Seven Bridges of Königsberg which is regarded as the first paper of Graph Theory. Since then,
       people have come to realize that if we can convert any problem to this City-Road problem, we can solve it easily by
       Graph Theory.

       Graph Theory has many applications.One of the most common application is to find the shortest distance between
       one city to another. We all know that to reach your PC, this web-page had to travel many routers from the server.
       Graph Theory helps it to find out the routers that needed to be crossed. During war, which street needs to be
       bombarded to disconnect the capital city from others, that too can be found out using Graph Theory.



       colegiohispanomexicano.net – Algorithms Notes                                                            33
   32   33   34   35   36   37   38   39   40   41   42