Question: Prove that every connected graph has a vertex whose removal (including all adjacent edges) will not disconnect the graph, and write a DFS method that finds such a vertex. Hint: Consider a vertex whose adjacent vertices are all marked.

Answer:
4.1.10

Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph.

Proof by contradiction:
If the graph has a node of degree one, removing it gives a connected graph.
Example: o - o

Otherwise, every path in the graph belongs to a cycle. To see this, start with any path, then notice that the terminal nodes of this path must be connected to other nodes that are not in the path, so we add them to the path to make a new path. Since the graph is connected, continuing this process it is possible to see that in the end all the nodes from the graph will be in the path.

If among all those paths there is no path from which one can remove a node without disconnecting the graph, then all those paths are bridges. In this case, they do not belong to a cycle, which is a contradiction (assuming the graph is finite).

Based on: https://math.stackexchange.com/questions/891325/proof-verification-a-connected-graph-always-has-a-vertex-that-is-not-a-cut-vert


Question: Draw the tree represented by edgeTo[] after the call bfs(G, 0) in ALGORITHM 4.2 for the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see Exercise 4.1.2).

Answer:
4.1.11

Tree represented by edgeTo[] after call to bfs(G, 0):

         0
     5   2   6
  10    3


Question: What does the BFS tree tell us about the distance from v to w when neither is at the root?

Answer:
4.1.12

When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch.
If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance from the root to vertex v and Dw is the distance from the root to vertex w.


Question: Suppose you use a stack instead of a queue when running breadth-first search. Does it still compute shortest paths?

Answer:
4.1.14

If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths.
This can be seen in the following graph:

  0 (source)
 / \
1 - 2
|   |
4 - 3

If the edge 0 - 2 is inserted before the edge 0 - 1:
Using a stack, the distance from 0 to 4 will be 3.
Using a queue, the distance from 0 to 4 will be 2.

If the edge 0 - 1 is inserted before the edge 0 - 2:
Using a stack, the distance from 0 to 3 will be 3.
Using a queue, the distance from 0 to 3 will be 2.

Thanks to lemonadeseason (https://github.com/lemonadeseason) for correcting the example in this exercise.
https://github.com/reneargento/algorithms-sedgewick-wayne/issues/24


Question: Show, in the style of the figure on page 545, a detailed trace of CC for finding the connected components in the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2).

Answer:
4.1.19
                   count            marked[]                       id[]
                           0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)               0     T                            0
 dfs(5)              0     T         T                  0         0
  check 0
  dfs(10)            0     T         T         T        0         0         0
   check 5
   dfs(3)            0     T     T   T         T        0     0   0         0
    check 10
    dfs(6)           0     T     T   T T       T        0     0   0 0       0
     dfs(2)          0     T   T T   T T       T        0   0 0   0 0       0
      check 5
      check 6
      check 0
      check 3
     2 done
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done
dfs(1)               1     T T T T   T T       T        0 1 0 0   0 0       0
 dfs(4)              1     T T T T T T T       T        0 1 0 0 1 0 0       0
  check 1
  dfs(8)             1     T T T T T T T   T   T        0 1 0 0 1 0 0   1   0
   check 1
   dfs(11)           1     T T T T T T T   T   T  T     0 1 0 0 1 0 0   1   0  1
    check 8
    dfs(7)           1     T T T T T T T T T   T  T     0 1 0 0 1 0 0 1 1   0  1
     check 8
     check 11
    7 done
    check 1
   11 done
   check 7
   check 4
  8 done
 4 done
 check 8
 check 11
1 done
dfs(9)               2     T T T T T T T T T T T  T     0 1 0 0 1 0 0 1 1 2 0  1
9 done


Question: Show, in the style of the figures in this section, a detailed trace of Cycle for finding a cycle in the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2). What is the order of growth of the running time of the Cycle constructor, in the worst case?

Answer:
4.1.20
             Has Cycle?             marked[]
                  F        0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                     T
 dfs(5)                    T         T
  check 0         F
  dfs(10)                  T         T         T
   check 5        F
   dfs(3)                  T     T   T         T
    check 10      F
    dfs(6)                 T     T   T T       T
     dfs(2)                T   T T   T T       T
      check 5     T (cycle found here)
      check 6     T
      check 0     T
      check 3     T
     2 done
     check 3      T
     check 0      T
    6 done
    check 2       T
   3 done
  10 done
  check 2         T
 5 done
 check 2          T
 check 6          T
0 done
dfs(1)                     T T T T   T T       T
 dfs(4)                    T T T T T T T       T
  check 1         T
  dfs(8)                   T T T T T T T   T   T
   check 1        T
   dfs(11)                 T T T T T T T   T   T  T
    check 8       T
    dfs(7)                 T T T T T T T T T   T  T
     check 8      T
     check 11     T
    7 done
    check 1       T
   11 done
   check 7        T
   check 4        T
  8 done
 4 done
 check 8          T
 check 11         T
1 done
dfs(9)                     T T T T T T T T T T T  T
9 done

The order of growth of the running time of the Cycle constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] array takes time proportional to V.


Question: Show, in the style of the figures in this section, a detailed trace of TwoColor for finding a two-coloring of the graph built by Graph’s input stream constructor for the file tinyGex2.txt (see EXERCISE 4.1.2). What is the order of growth of the running time of the TwoColor constructor, in the worst case?

Answer:
4.1.21
               Is 2-colorable?                marked[]                     color[]
                     T               0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                               T                            F F F F F F F F F F F  F
 dfs(5)                              T         T                  F F F F F T F F F F F  F
  check 0            T
  dfs(10)                            T         T         T        F F F F F T F F F F F  F
   check 5           T
   dfs(3)                            T     T   T         T        F F F T F T F F F F F  F
    check 10         T
    dfs(6)                           T     T   T T       T        F F F T F T F F F F F  F
     dfs(2)                          T   T T   T T       T        F F T T F T F F F F F  F
      check 5        F
      check 6        F
      check 0        F
      check 3        F
     2 done
     check 3         F
     check 0         F
    6 done
    check 2          F
   3 done
  10 done
  check 2            F
 5 done
 check 2             F
 check 6             F
0 done
dfs(1)                               T T T T   T T       T        F F T T F T F F F F F  F
 dfs(4)                              T T T T T T T       T        F F T T T T F F F F F  F
  check 1            F
  dfs(8)                             T T T T T T T   T   T        F F T T F T F F F F F  F
   check 1           F
   dfs(11)                           T T T T T T T   T   T  T     F F T T F T F F F F F  T
    check 8          F
    dfs(7)                           T T T T T T T T T   T  T     F F T T F T F F F F F  F
     check 8         F
     check 11        F
    7 done
    check 1          F
   11 done
   check 7           F
   check 4           F
  8 done
 4 done
 check 8             F
 check 11            F
1 done
dfs(9)                               T T T T T T T T T T T  T     F F T T F T F F F F F  F
9 done

The order of growth of the running time of the TwoColor constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] and color[] arrays takes time proportional to V.


Question: Compute the number of connected components in movies.txt, the size of the largest component, and the number of components of size less than 10. Find the eccentricity, diameter, radius, a center, and the girth of the largest component in the graph. Does it contain Kevin Bacon?

Answer:
4.1.24

Number of connected components: 33
Size of the largest component: 118774
Number of components of size less than 10: 5
Does the largest component contain Kevin Bacon: Yes

Eccentricity, diameter, radius, center and girth:

The strategy for computing the eccentricity, diameter, radius, center and girth of the largest component was the following:
The algorithm required for these exact computations has complexity of O(V * E) (running breadth-first search from all vertices). V is ~= 10^5, which means the algorithm required for these exact computation has complexity ~= 10^10.

In order to reduce the complexity of the computations (and to be able to compute them), domain knowledge was used to compute approximate results.
Based on the Kevin Bacon game, we know that Kevin Bacon has been on several movies and that he is closely connected to most of the actors and actresses in the graph. Therefore, he has a high probability of being the center of the graph.

Using Kevin Bacon as the center, we compute his vertex eccentricity to get the graph radius.
A breadth-first search using Kevin Bacon as the source computes the vertices that are furthest from the center. Computing the eccentricities of these vertices we can find the diameter of the graph.

The eccentricities of the center and of the vertices furthest from it are shown in the results. The range of the eccentricities  is [10, 18]. Computing the eccentricities of all vertices would bring us back to the original problem of =~ 10^10 operations.

Finally, for the girth, we know that there is a very high probability that two actors have worked together on two different movies. This gives a girth of 4, which is the minimum girth possible for the movies graph:
Actor -- Movie -- Actor
    \            /
     \   Movie  /
To validate this theory we run the algorithm to compute the girth of the graph but stop once we find a cycle of length 4, since it is the shortest cycle possible.

Eccentricities of Kevin Bacon and of vertices furthest from the center in the largest component:
Eccentricity of vertex 22970: 16
Eccentricity of vertex 22971: 16
Eccentricity of vertex 22972: 16
Eccentricity of vertex 22973: 16
Eccentricity of vertex 22974: 16
Eccentricity of vertex 22976: 16
Eccentricity of vertex 22977: 16
Eccentricity of vertex 22978: 16
Eccentricity of vertex 22979: 16
Eccentricity of vertex 22980: 16
Eccentricity of vertex 51437: 18
Eccentricity of vertex 51438: 18
Eccentricity of vertex 51439: 18
Eccentricity of vertex 51440: 18
Eccentricity of vertex 51441: 18
Eccentricity of vertex 51442: 18
Eccentricity of vertex 51443: 18
Eccentricity of vertex 51444: 18
Eccentricity of vertex 51445: 18
Eccentricity of vertex 51446: 18
Eccentricity of vertex 51447: 18
Eccentricity of vertex 51448: 18
Eccentricity of vertex 51449: 18
Eccentricity of vertex 51450: 18
Eccentricity of vertex 51451: 18
Eccentricity of vertex 51452: 18
Eccentricity of vertex 51453: 18
Eccentricity of vertex 51454: 18
Eccentricity of vertex 51455: 18
Eccentricity of vertex 51456: 18
Eccentricity of vertex 51457: 18
Eccentricity of vertex 51458: 18
Eccentricity of vertex 51459: 18
Eccentricity of vertex 51460: 18
Eccentricity of vertex 51461: 18
Eccentricity of vertex 51462: 18
Eccentricity of vertex 51463: 18
Eccentricity of vertex 51464: 18
Eccentricity of vertex 51465: 18
Eccentricity of vertex 51466: 18
Eccentricity of vertex 51467: 18
Eccentricity of vertex 51468: 18
Eccentricity of vertex 51469: 18
Eccentricity of vertex 51470: 18
Eccentricity of vertex 51471: 18
Eccentricity of vertex 51472: 18
Eccentricity of vertex 51473: 18
Eccentricity of vertex 51474: 18
Eccentricity of vertex 51475: 18
Eccentricity of vertex 51476: 18
Eccentricity of vertex 51477: 18
Eccentricity of vertex 51478: 18
Eccentricity of vertex 51479: 18
Eccentricity of vertex 51480: 18
Eccentricity of vertex 51481: 18
Eccentricity of vertex 51482: 18
Eccentricity of vertex 51483: 18
Eccentricity of vertex 51484: 18
Eccentricity of vertex 51485: 18
Eccentricity of vertex 51486: 18
Eccentricity of vertex 51487: 18
Eccentricity of vertex 51488: 18
Eccentricity of vertex 51489: 18
Eccentricity of vertex 51490: 18
Eccentricity of vertex 51491: 18
Eccentricity of vertex 51492: 18
Eccentricity of vertex 51493: 18
Eccentricity of vertex 51494: 18
Eccentricity of vertex 51495: 18
Eccentricity of vertex 51496: 18
Eccentricity of vertex 51497: 18
Eccentricity of vertex 51498: 18
Eccentricity of vertex 51499: 18
Eccentricity of vertex 51500: 18
Eccentricity of vertex 51501: 18
Eccentricity of vertex 51502: 18
Eccentricity of vertex 51503: 18
Eccentricity of vertex 51504: 18
Eccentricity of vertex 51505: 18
Eccentricity of vertex 51506: 18
Eccentricity of vertex 51507: 18
Eccentricity of vertex 51508: 18
Eccentricity of vertex 51509: 18
Eccentricity of vertex 51510: 18
Eccentricity of vertex 51511: 18
Eccentricity of vertex 51512: 18
Eccentricity of vertex 51513: 18
Eccentricity of vertex 51514: 18
Eccentricity of vertex 51515: 18
Eccentricity of vertex 51516: 18
Eccentricity of vertex 51517: 18
Eccentricity of vertex 51518: 18
Eccentricity of vertex 51519: 18
Eccentricity of vertex 51520: 18
Eccentricity of vertex 86241: 16
Eccentricity of vertex 86242: 16
Eccentricity of vertex 86243: 16
Eccentricity of vertex 86244: 16
Eccentricity of vertex 86245: 16
Eccentricity of vertex 86246: 16
Eccentricity of vertex 86247: 16
Eccentricity of vertex 86248: 16
Eccentricity of vertex 86249: 16
Eccentricity of vertex 86250: 16
Eccentricity of vertex 86251: 16
Eccentricity of vertex 86252: 16
Eccentricity of vertex 86253: 16
Eccentricity of vertex 86254: 16
Eccentricity of vertex 86255: 16
Eccentricity of vertex 86256: 16
Eccentricity of vertex 86257: 16
Eccentricity of vertex 86258: 16
Eccentricity of vertex 86259: 16
Eccentricity of vertex 118353: 18
Eccentricity of vertex 118354: 18
Eccentricity of vertex 118355: 18
Eccentricity of vertex 118356: 18
Eccentricity of vertex 118357: 18
Eccentricity of vertex 118358: 18
Eccentricity of vertex 118359: 18
Eccentricity of vertex 118360: 18
Eccentricity of vertex 118361: 18
Eccentricity of vertex 118362: 18
Eccentricity of vertex 118363: 18
Eccentricity of vertex 118364: 18
Eccentricity of vertex 9145: 10

Diameter of largest component: 18
Radius of largest component: 10
Center of largest component: 9145
Girth of largest component: 4


Question: Determine the amount of memory used by Graph to represent a graph with V vertices and E edges, using the memory-cost model of SECTION 1.4.

Answer:
4.1.27

Integer
* object overhead -> 16 bytes
* int value -> 4 bytes
* padding -> 4 bytes
Amount of memory needed: 16 + 4 + 4 = 24 bytes

Node
* object overhead -> 16 bytes
* extra overhead for reference to the enclosing instance -> 8 bytes
* Item reference (item) -> 8 bytes
* Node reference (next) -> 8 bytes
Amount of memory needed: 16 + 8 + 8 + 8 = 40 bytes

Bag
* object overhead -> 16 bytes
* Node reference (first) -> 8 bytes
* int value (size) -> 4 bytes
* padding -> 4 bytes
* N Nodes -> 40N bytes
* Integer (item) -> 24N bytes
Amount of memory needed: 16 + 8 + 4 + 4 + 40N + 24N = 64N + 32 bytes

Graph
* object overhead -> 16 bytes
* int value (V) -> 4 bytes
* int value (E) -> 4 bytes
* Bag<Integer>[] reference (adj) -> 8 bytes
* Bag<Integer>[] (adj)
   object overhead -> 16 bytes
   int value (length) -> 4 bytes
   padding -> 4 bytes
   Bag references -> 8V
   Bag -> 64E + 32 bytes -> There are V Bags and in total, they have 2E nodes -> 128E + 32V
Amount of memory needed: 16 + 4 + 4 + 8 + 16 + 4 + 4 + 8V + 128E + 32V = 128E + 40V + 56 bytes


Question: Two graphs are isomorphic if there is a way to rename the vertices of one to make it identical to the other. Draw all the nonisomorphic graphs with two, three, four, and five vertices.

Answer:
4.1.28

Non-isomorphic graphs:

There are 2 non-isomorphic graphs with 2 vertices:
o o

o-o

There are 4 non-isomorphic graphs with 3 vertices:
 o
o o

 o
o-o

o-o-o

 o
/ \
o-o

There are 11 non-isomorphic graphs with 4 vertices:
o o o o

o-o o o

o-o-o o

o-o-o-o

o-o o-o

  o
  |
  o
 / \
o   o

  o
 / \
o---o o

o-o
| |
o-o

  o
  |
  o
 / \
o---o

  o
 / \
o---o
 \ /
  o

   o
  /|\
 / o \
/ / \ \
o------o

There are 34 non-isomorphic graphs with 5 vertices:
  o
o   o
 o o

  o
o   o
 o-o

  o
o    o
    /
 o-o

   o
o     o
 \   /
  o o

   o
 / | \
o  o  o
     o

  o
 / \
o   o
 o-o

   o
 /  \
o    o
 \
  o o

   o
 /  \
o----o
  o o

   o
 /|\\
o o oo

o-o
| |
o-o o

o
|
o-o
| |
o o

  o
  |
  o
 / \
o---o o

o-o-o-o-o

  o  o
 /|  |
o |  |
 \|  |
  o  o

o--o
|  |
o--o
|
o

o   o
|   |
o---o
 \ /
  o

 o--o
  \/
o--o--o

   o
 /  \
o    o
 \   /
  o-o

    o   (This is a complete graph, where all vertices have degree = 4)
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o---o

    o
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o   o

o---o
|\ /|\
| X | o
|/ \|/
o---o

o---o
|\ /|
| o |
|/ \|
o---o

o---o
|\ /|\
| X | o
|/ \|
o---o

   o
 // \
o-o  o
   \/
    o

o-o-o-o
 \| | /
   o

    o
   /\
  /  \
 /    \
o------o
\  \ / /
 \ /\ /
  o  o

o---o
| X | o
o---o

o   o
|\ /|
| o |
|/ \|
o   o

     o
    /|\
o--o | o
    \|/
     o

  o
  |
  o
 /|\
o | o
 \|/
  o

  o
 / \
o---o
|   |
o---o

o---o
|  /|
| o |
|/  |
o---o

  o
  |
  o
  |
  o
 / \
o---o

o---o
| \ |
o---o o

Based on: http://www.graphclasses.org/smallgraphs.html


Question:
Eulerian and Hamiltonian cycles. Consider the graphs defined by the following four sets of edges:
0-1 0-2 0-3 1-3 1-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8
0-1 0-2 0-3 1-3 0-3 2-5 5-6 3-6 4-7 4-8 5-8 5-9 6-7 6-9 8-8
0-1 1-2 1-3 0-3 0-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8
4-1 7-9 6-2 7-3 5-0 0-2 0-8 1-6 3-9 6-3 2-8 1-5 9-8 4-5 4-7
Which of these graphs have Euler cycles (cycles that visit each edge exactly once)? Which of them have Hamilton cycles (cycles that visit each vertex exactly once)?

Answer:
4.1.30 - Eulerian and Hamiltonian cycles

An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once.
A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visited twice).

According to Euler theorems a graph has an Eulerian cycle/circuit if and only if it does not have any vertices of odd degree.

First graph:
0-1 0-2 0-3 1-3 1-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9).
It has a Hamiltonian cycle: 1-4 4-8 8-7 7-6 6-9 9-5 5-2 2-0 0-3 3-1

Second graph:
0-1 0-2 0-3 1-3 0-3 2-5 5-6 3-6 4-7 4-8 5-8 5-9 6-7 6-9 8-8

It has an Eulerian cycle (all the vertices have even degrees):
0-3 3-0 0-2 2-5 5-9 9-6 6-5 5-8 8-8 8-4 4-7 7-6 6-3 3-1 1-0
There is no Hamiltonian cycle.

Third graph:
0-1 1-2 1-3 0-3 0-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 4-8 8-7 7-6 6-9 9-5 5-2 2-1 1-3 3-0 0-4

Fourth graph:
4-1 7-9 6-2 7-3 5-0 0-2 0-8 1-6 3-9 6-3 2-8 1-5 9-8 4-5 4-7

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 0-5 5-4 4-1 1-6 6-3 3-7 7-9 9-8 8-2 2-0


Question: Graph enumeration. How many different undirected graphs are there with V vertices and E edges (and no parallel edges)?

Answer:
4.1.31 - Graph enumeration

If we were considering graphs with no self-loops:

There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect.
          (2)
So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)!
             ((2))
             ( E )
different undirected graphs with V vertices and E edges (and no parallel edges and no self-loops).

Since we are also considering graphs with self-loops, there are (V) * 2^V ways to choose a set {u, v} of two vertices in which
                                                                (2)
vertices may or may not have a self-loop.

So there are ((V) * 2^V) = ((V! / 2! * (V - 2)!) * 2^V)! / E! * (((V! / 2! * (V - 2)!) * 2^V) - E)!
             ((2)      )
             (    E    )
different undirected graphs with V vertices and E edges (and no parallel edges).

Reference:
Handbook of Discrete and Combinatorial Mathematics by Kenneth H. Rosen, page 580
https://math.stackexchange.com/questions/1072726/counting-simple-connected-labeled-graphs-with-n-vertices-and-k-edges
https://math.stackexchange.com/questions/128439/how-to-determine-the-number-of-directed-undirected-graphs


Question: Odd cycles. Prove that a graph is two-colorable (bipartite) if and only if it contains no odd-length cycle.

Answer:
4.1.33 - Odd cycles

A graph is two-colorable (bipartite) if and only if it contains no odd-length cycle.

Proof:
1- Proving that a graph with an odd-length cycle cannot be bipartite:
If a graph G is bipartite with vertex sets V1 and V2, every step along a walk takes you either from V1 to V2 or from V2 to V1. To end up where you started, therefore, you must take an even number of steps.

2- Proving that a graph with only even-length cycles is bipartite:
Consider G to be a graph with only even-length cycles. Let v0 be any vertex. For each vertex v in the same component C0 as v0 let d(v) be the length of the shortest path from v0 to v. Color red every vertex in C0 whose distance from v0 is even, and color the other vertices of C0 blue. Do the same for each component of G. Check that if G had any edge between two red vertices or between two blue vertices, it would have an odd cycle. Thus, G is bipartite, the red vertices and the blue vertices being the two parts.

Reference:
https://math.stackexchange.com/questions/311665/proof-a-graph-is-bipartite-if-and-only-if-it-contains-no-odd-cycles


Question: Biconnectedness. A graph is biconnected if every pair of vertices is connected by two disjoint paths. An articulation point in a connected graph is a vertex that would disconnect the graph if it (and its adjacent edges) were removed. Prove that any graph with no articulation points is biconnected. Hint: Given a pair of vertices s and t and a path connecting them, use the fact that none of the vertices on the path are articulation points to construct two disjoint paths connecting s and t.

Answer:
4.1.35 - Biconnectedness

Any graph with no articulation points is biconnected.

Proof:
Consider two vertices, s, t, and a path P1 connecting s to t.
We know that no vertex in P1 is an articulation point, so for each vertex v in the path, there is always another path P2 connecting s to t that does not include it. Also, P2 does not include any of the vertices of P1, otherwise any vertex included in both paths would be an articulation point (being the only way to connect s to t).
This means that every pair of vertices is connected by two vertex-disjoint paths (such as P1 and P2), making the graph biconnected.

Graph illustration (P1 is the path s-v1 v1-v2 v2-t and P2 is the path s-v3 v3-v4 v4-t):

s
|\
| \
v1 v3
|   \
|   v4
v2 /
| /
|/
t


Question: What is the maximum number of edges in a digraph with V vertices and no parallel edges? What is the minimum number of edges in a digraph with V vertices, none of which are isolated?

Answer:
4.1.1

The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2.
Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide the result by 2.

Example:
o - o
| X |
o — o

The minimum number of edges in a graph with V vertices, none of which are isolated (have degree 0) is V - 1.
Example:
o — o — o


Question: Draw, in the style of the figure in the text (page 524), the adjacency lists built by Digraph’s input stream constructor for the file tinyDGex2.txt depicted at left.

Answer:
4.1.2

adj[]
  0  -> 5 -> 2 -> 6
  1  -> 4 -> 8 -> 11
  2  -> 5 -> 6 -> 0 -> 3
  3  -> 10 -> 6 -> 2
  4  -> 1 -> 8
  5  -> 0 -> 10 -> 2
  6  -> 2 -> 3 -> 0
  7  -> 8 -> 11
  8  -> 1 -> 11 -> 7 -> 4
  9  ->
 10  -> 5 -> 3
 11  -> 8 -> 7 -> 1


Question: Develop a test client for Digraph.

Answer:
4.1.6

The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections.

adj[]
  0  -> 1 -> 3 (the original was 0 -> 3 -> 1)
  1  -> 2 -> 0
  2  -> 3 -> 1
  3  -> 0 -> 2


Question: Write a method that checks whether or not a given permutation of a DAG’s vertices is a topological order of that DAG.

Answer:
4.1.9
                marked[]    adj[]
dfs (0)          0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5          5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

 dfs (5)         0 T        0 5 2 6
  check 0        1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

  dfs (10)       0 T        0 5 2 6
   check 5       1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

   dfs (3)       0 T        0 5 2 6
    check 10     1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

    dfs (6)      0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

     dfs (2)     0 T        0 5 2 6
      check 5    1          1 4 8 11
      check 6    2 T        2 5 6 0 3
      check 0    3 T        3 10 6 2
      check 3    4          4 1 8
     2 done      5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done

edgeTo[] tree

           0
          5
        10
       3
      6
     2


Question: Given a DAG, does there exist a topological order that cannot result from applying a DFS-based algorithm, no matter in what order the vertices adjacent to each vertex are chosen? Prove your answer.

Answer:
4.1.10

Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph.

Proof by contradiction:
If the graph has a node of degree one, removing it gives a connected graph.
Example: o - o

Otherwise, every path in the graph belongs to a cycle. To see this, start with any path, then notice that the terminal nodes of this path must be connected to other nodes that are not in the path, so we add them to the path to make a new path. Since the graph is connected, continuing this process it is possible to see that in the end all the nodes from the graph will be in the path.

If among all those paths there is no path from which one can remove a node without disconnecting the graph, then all those paths are bridges. In this case, they do not belong to a cycle, which is a contradiction (assuming the graph is finite).

Based on: https://math.stackexchange.com/questions/891325/proof-verification-a-connected-graph-always-has-a-vertex-that-is-not-a-cut-vert


Question: Describe a family of sparse digraphs whose number of directed cycles grows exponentially in the number of vertices.

Answer:
4.1.11

Tree represented by edgeTo[] after call to bfs(G, 0):

         0
     5   2   6
  10    3


Question: How many edges are there in the transitive closure of a digraph that is a simple directed path with V vertices and V–1 edges?

Answer:
4.1.12

When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch.
If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance from the root to vertex v and Dw is the distance from the root to vertex w.


Question: Prove that the strong components in G^R are the same as in G.

Answer:
4.1.14

If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths.
This can be seen in the following graph:

  0 (source)
 / \
1 - 2
|   |
4 - 3

If the edge 0 - 2 is inserted before the edge 0 - 1:
Using a stack, the distance from 0 to 4 will be 3.
Using a queue, the distance from 0 to 4 will be 2.

If the edge 0 - 1 is inserted before the edge 0 - 2:
Using a stack, the distance from 0 to 3 will be 3.
Using a queue, the distance from 0 to 3 will be 2.

Thanks to lemonadeseason (https://github.com/lemonadeseason) for correcting the example in this exercise.
https://github.com/reneargento/algorithms-sedgewick-wayne/issues/24


Question: Topological sort and BFS. Explain why the following algorithm does not necessarily produce a topological order: Run BFS, and label the vertices by increasing distance to their respective source.

Answer:
4.1.19
                   count            marked[]                       id[]
                           0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)               0     T                            0
 dfs(5)              0     T         T                  0         0
  check 0
  dfs(10)            0     T         T         T        0         0         0
   check 5
   dfs(3)            0     T     T   T         T        0     0   0         0
    check 10
    dfs(6)           0     T     T   T T       T        0     0   0 0       0
     dfs(2)          0     T   T T   T T       T        0   0 0   0 0       0
      check 5
      check 6
      check 0
      check 3
     2 done
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done
dfs(1)               1     T T T T   T T       T        0 1 0 0   0 0       0
 dfs(4)              1     T T T T T T T       T        0 1 0 0 1 0 0       0
  check 1
  dfs(8)             1     T T T T T T T   T   T        0 1 0 0 1 0 0   1   0
   check 1
   dfs(11)           1     T T T T T T T   T   T  T     0 1 0 0 1 0 0   1   0  1
    check 8
    dfs(7)           1     T T T T T T T T T   T  T     0 1 0 0 1 0 0 1 1   0  1
     check 8
     check 11
    7 done
    check 1
   11 done
   check 7
   check 4
  8 done
 4 done
 check 8
 check 11
1 done
dfs(9)               2     T T T T T T T T T T T  T     0 1 0 0 1 0 0 1 1 2 0  1
9 done


Question: Directed Eulerian cycle. An Eulerian cycle is a directed cycle that contains each edge exactly once. Write a graph client Euler that finds an Eulerian cycle or reports that no such tour exists. Hint: Prove that a digraph G has a directed Eulerian cycle if and only if G is connected and each vertex has its indegree equal to its outdegree.

Answer:
4.1.20
             Has Cycle?             marked[]
                  F        0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                     T
 dfs(5)                    T         T
  check 0         F
  dfs(10)                  T         T         T
   check 5        F
   dfs(3)                  T     T   T         T
    check 10      F
    dfs(6)                 T     T   T T       T
     dfs(2)                T   T T   T T       T
      check 5     T (cycle found here)
      check 6     T
      check 0     T
      check 3     T
     2 done
     check 3      T
     check 0      T
    6 done
    check 2       T
   3 done
  10 done
  check 2         T
 5 done
 check 2          T
 check 6          T
0 done
dfs(1)                     T T T T   T T       T
 dfs(4)                    T T T T T T T       T
  check 1         T
  dfs(8)                   T T T T T T T   T   T
   check 1        T
   dfs(11)                 T T T T T T T   T   T  T
    check 8       T
    dfs(7)                 T T T T T T T T T   T  T
     check 8      T
     check 11     T
    7 done
    check 1       T
   11 done
   check 7        T
   check 4        T
  8 done
 4 done
 check 8          T
 check 11         T
1 done
dfs(9)                     T T T T T T T T T T T  T
9 done

The order of growth of the running time of the Cycle constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] array takes time proportional to V.


Question: LCA of a DAG. Given a DAG and two vertices v and w, find the lowest common ancestor (LCA) of v and w. The LCA of v and w is an ancestor of v and w that has no descendants that are also ancestors of v and w. Computing the LCA is useful in multiple inheritance in programming languages, analysis of genealogical data (find degree of inbreeding in a pedigree graph), and other applications. Hint: Define the height of a vertex v in a DAG to be the length of the longest path from a root to v. Among vertices that are ancestors of both v and w, the one with the greatest height is an LCA of v and w.

Answer:
4.1.21
               Is 2-colorable?                marked[]                     color[]
                     T               0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                               T                            F F F F F F F F F F F  F
 dfs(5)                              T         T                  F F F F F T F F F F F  F
  check 0            T
  dfs(10)                            T         T         T        F F F F F T F F F F F  F
   check 5           T
   dfs(3)                            T     T   T         T        F F F T F T F F F F F  F
    check 10         T
    dfs(6)                           T     T   T T       T        F F F T F T F F F F F  F
     dfs(2)                          T   T T   T T       T        F F T T F T F F F F F  F
      check 5        F
      check 6        F
      check 0        F
      check 3        F
     2 done
     check 3         F
     check 0         F
    6 done
    check 2          F
   3 done
  10 done
  check 2            F
 5 done
 check 2             F
 check 6             F
0 done
dfs(1)                               T T T T   T T       T        F F T T F T F F F F F  F
 dfs(4)                              T T T T T T T       T        F F T T T T F F F F F  F
  check 1            F
  dfs(8)                             T T T T T T T   T   T        F F T T F T F F F F F  F
   check 1           F
   dfs(11)                           T T T T T T T   T   T  T     F F T T F T F F F F F  T
    check 8          F
    dfs(7)                           T T T T T T T T T   T  T     F F T T F T F F F F F  F
     check 8         F
     check 11        F
    7 done
    check 1          F
   11 done
   check 7           F
   check 4           F
  8 done
 4 done
 check 8             F
 check 11            F
1 done
dfs(9)                               T T T T T T T T T T T  T     F F T T F T F F F F F  F
9 done

The order of growth of the running time of the TwoColor constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] and color[] arrays takes time proportional to V.


Question:
Hamiltonian path in DAGs. Given a DAG, design a linear-time algorithm to determine whether there is a directed path that visits each vertex exactly once.
Answer: Compute a topological sort and check if there is an edge between each consecutive pair of vertices in the topological order.

Answer:
4.1.24

Number of connected components: 33
Size of the largest component: 118774
Number of components of size less than 10: 5
Does the largest component contain Kevin Bacon: Yes

Eccentricity, diameter, radius, center and girth:

The strategy for computing the eccentricity, diameter, radius, center and girth of the largest component was the following:
The algorithm required for these exact computations has complexity of O(V * E) (running breadth-first search from all vertices). V is ~= 10^5, which means the algorithm required for these exact computation has complexity ~= 10^10.

In order to reduce the complexity of the computations (and to be able to compute them), domain knowledge was used to compute approximate results.
Based on the Kevin Bacon game, we know that Kevin Bacon has been on several movies and that he is closely connected to most of the actors and actresses in the graph. Therefore, he has a high probability of being the center of the graph.

Using Kevin Bacon as the center, we compute his vertex eccentricity to get the graph radius.
A breadth-first search using Kevin Bacon as the source computes the vertices that are furthest from the center. Computing the eccentricities of these vertices we can find the diameter of the graph.

The eccentricities of the center and of the vertices furthest from it are shown in the results. The range of the eccentricities  is [10, 18]. Computing the eccentricities of all vertices would bring us back to the original problem of =~ 10^10 operations.

Finally, for the girth, we know that there is a very high probability that two actors have worked together on two different movies. This gives a girth of 4, which is the minimum girth possible for the movies graph:
Actor -- Movie -- Actor
    \            /
     \   Movie  /
To validate this theory we run the algorithm to compute the girth of the graph but stop once we find a cycle of length 4, since it is the shortest cycle possible.

Eccentricities of Kevin Bacon and of vertices furthest from the center in the largest component:
Eccentricity of vertex 22970: 16
Eccentricity of vertex 22971: 16
Eccentricity of vertex 22972: 16
Eccentricity of vertex 22973: 16
Eccentricity of vertex 22974: 16
Eccentricity of vertex 22976: 16
Eccentricity of vertex 22977: 16
Eccentricity of vertex 22978: 16
Eccentricity of vertex 22979: 16
Eccentricity of vertex 22980: 16
Eccentricity of vertex 51437: 18
Eccentricity of vertex 51438: 18
Eccentricity of vertex 51439: 18
Eccentricity of vertex 51440: 18
Eccentricity of vertex 51441: 18
Eccentricity of vertex 51442: 18
Eccentricity of vertex 51443: 18
Eccentricity of vertex 51444: 18
Eccentricity of vertex 51445: 18
Eccentricity of vertex 51446: 18
Eccentricity of vertex 51447: 18
Eccentricity of vertex 51448: 18
Eccentricity of vertex 51449: 18
Eccentricity of vertex 51450: 18
Eccentricity of vertex 51451: 18
Eccentricity of vertex 51452: 18
Eccentricity of vertex 51453: 18
Eccentricity of vertex 51454: 18
Eccentricity of vertex 51455: 18
Eccentricity of vertex 51456: 18
Eccentricity of vertex 51457: 18
Eccentricity of vertex 51458: 18
Eccentricity of vertex 51459: 18
Eccentricity of vertex 51460: 18
Eccentricity of vertex 51461: 18
Eccentricity of vertex 51462: 18
Eccentricity of vertex 51463: 18
Eccentricity of vertex 51464: 18
Eccentricity of vertex 51465: 18
Eccentricity of vertex 51466: 18
Eccentricity of vertex 51467: 18
Eccentricity of vertex 51468: 18
Eccentricity of vertex 51469: 18
Eccentricity of vertex 51470: 18
Eccentricity of vertex 51471: 18
Eccentricity of vertex 51472: 18
Eccentricity of vertex 51473: 18
Eccentricity of vertex 51474: 18
Eccentricity of vertex 51475: 18
Eccentricity of vertex 51476: 18
Eccentricity of vertex 51477: 18
Eccentricity of vertex 51478: 18
Eccentricity of vertex 51479: 18
Eccentricity of vertex 51480: 18
Eccentricity of vertex 51481: 18
Eccentricity of vertex 51482: 18
Eccentricity of vertex 51483: 18
Eccentricity of vertex 51484: 18
Eccentricity of vertex 51485: 18
Eccentricity of vertex 51486: 18
Eccentricity of vertex 51487: 18
Eccentricity of vertex 51488: 18
Eccentricity of vertex 51489: 18
Eccentricity of vertex 51490: 18
Eccentricity of vertex 51491: 18
Eccentricity of vertex 51492: 18
Eccentricity of vertex 51493: 18
Eccentricity of vertex 51494: 18
Eccentricity of vertex 51495: 18
Eccentricity of vertex 51496: 18
Eccentricity of vertex 51497: 18
Eccentricity of vertex 51498: 18
Eccentricity of vertex 51499: 18
Eccentricity of vertex 51500: 18
Eccentricity of vertex 51501: 18
Eccentricity of vertex 51502: 18
Eccentricity of vertex 51503: 18
Eccentricity of vertex 51504: 18
Eccentricity of vertex 51505: 18
Eccentricity of vertex 51506: 18
Eccentricity of vertex 51507: 18
Eccentricity of vertex 51508: 18
Eccentricity of vertex 51509: 18
Eccentricity of vertex 51510: 18
Eccentricity of vertex 51511: 18
Eccentricity of vertex 51512: 18
Eccentricity of vertex 51513: 18
Eccentricity of vertex 51514: 18
Eccentricity of vertex 51515: 18
Eccentricity of vertex 51516: 18
Eccentricity of vertex 51517: 18
Eccentricity of vertex 51518: 18
Eccentricity of vertex 51519: 18
Eccentricity of vertex 51520: 18
Eccentricity of vertex 86241: 16
Eccentricity of vertex 86242: 16
Eccentricity of vertex 86243: 16
Eccentricity of vertex 86244: 16
Eccentricity of vertex 86245: 16
Eccentricity of vertex 86246: 16
Eccentricity of vertex 86247: 16
Eccentricity of vertex 86248: 16
Eccentricity of vertex 86249: 16
Eccentricity of vertex 86250: 16
Eccentricity of vertex 86251: 16
Eccentricity of vertex 86252: 16
Eccentricity of vertex 86253: 16
Eccentricity of vertex 86254: 16
Eccentricity of vertex 86255: 16
Eccentricity of vertex 86256: 16
Eccentricity of vertex 86257: 16
Eccentricity of vertex 86258: 16
Eccentricity of vertex 86259: 16
Eccentricity of vertex 118353: 18
Eccentricity of vertex 118354: 18
Eccentricity of vertex 118355: 18
Eccentricity of vertex 118356: 18
Eccentricity of vertex 118357: 18
Eccentricity of vertex 118358: 18
Eccentricity of vertex 118359: 18
Eccentricity of vertex 118360: 18
Eccentricity of vertex 118361: 18
Eccentricity of vertex 118362: 18
Eccentricity of vertex 118363: 18
Eccentricity of vertex 118364: 18
Eccentricity of vertex 9145: 10

Diameter of largest component: 18
Radius of largest component: 10
Center of largest component: 9145
Girth of largest component: 4


Question: Digraph enumeration. Show that the number of different V-vertex digraphs with no parallel edges is 2^(V^2). (How many digraphs are there that contain V vertices and E edges?) Then compute an upper bound on the percentage of 20-vertex digraphs that could ever be examined by any computer, under the assumptions that every electron in the universe examines a digraph every nanosecond, that the universe has fewer than 10^80 electrons, and that the age of the universe will be less than 10^20 years.

Answer:
4.1.27

Integer
* object overhead -> 16 bytes
* int value -> 4 bytes
* padding -> 4 bytes
Amount of memory needed: 16 + 4 + 4 = 24 bytes

Node
* object overhead -> 16 bytes
* extra overhead for reference to the enclosing instance -> 8 bytes
* Item reference (item) -> 8 bytes
* Node reference (next) -> 8 bytes
Amount of memory needed: 16 + 8 + 8 + 8 = 40 bytes

Bag
* object overhead -> 16 bytes
* Node reference (first) -> 8 bytes
* int value (size) -> 4 bytes
* padding -> 4 bytes
* N Nodes -> 40N bytes
* Integer (item) -> 24N bytes
Amount of memory needed: 16 + 8 + 4 + 4 + 40N + 24N = 64N + 32 bytes

Graph
* object overhead -> 16 bytes
* int value (V) -> 4 bytes
* int value (E) -> 4 bytes
* Bag<Integer>[] reference (adj) -> 8 bytes
* Bag<Integer>[] (adj)
   object overhead -> 16 bytes
   int value (length) -> 4 bytes
   padding -> 4 bytes
   Bag references -> 8V
   Bag -> 64E + 32 bytes -> There are V Bags and in total, they have 2E nodes -> 128E + 32V
Amount of memory needed: 16 + 4 + 4 + 8 + 16 + 4 + 4 + 8V + 128E + 32V = 128E + 40V + 56 bytes


Question: DAG enumeration. Give a formula for the number of V-vertex DAGs with E edges.

Answer:
4.1.28

Non-isomorphic graphs:

There are 2 non-isomorphic graphs with 2 vertices:
o o

o-o

There are 4 non-isomorphic graphs with 3 vertices:
 o
o o

 o
o-o

o-o-o

 o
/ \
o-o

There are 11 non-isomorphic graphs with 4 vertices:
o o o o

o-o o o

o-o-o o

o-o-o-o

o-o o-o

  o
  |
  o
 / \
o   o

  o
 / \
o---o o

o-o
| |
o-o

  o
  |
  o
 / \
o---o

  o
 / \
o---o
 \ /
  o

   o
  /|\
 / o \
/ / \ \
o------o

There are 34 non-isomorphic graphs with 5 vertices:
  o
o   o
 o o

  o
o   o
 o-o

  o
o    o
    /
 o-o

   o
o     o
 \   /
  o o

   o
 / | \
o  o  o
     o

  o
 / \
o   o
 o-o

   o
 /  \
o    o
 \
  o o

   o
 /  \
o----o
  o o

   o
 /|\\
o o oo

o-o
| |
o-o o

o
|
o-o
| |
o o

  o
  |
  o
 / \
o---o o

o-o-o-o-o

  o  o
 /|  |
o |  |
 \|  |
  o  o

o--o
|  |
o--o
|
o

o   o
|   |
o---o
 \ /
  o

 o--o
  \/
o--o--o

   o
 /  \
o    o
 \   /
  o-o

    o   (This is a complete graph, where all vertices have degree = 4)
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o---o

    o
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o   o

o---o
|\ /|\
| X | o
|/ \|/
o---o

o---o
|\ /|
| o |
|/ \|
o---o

o---o
|\ /|\
| X | o
|/ \|
o---o

   o
 // \
o-o  o
   \/
    o

o-o-o-o
 \| | /
   o

    o
   /\
  /  \
 /    \
o------o
\  \ / /
 \ /\ /
  o  o

o---o
| X | o
o---o

o   o
|\ /|
| o |
|/ \|
o   o

     o
    /|\
o--o | o
    \|/
     o

  o
  |
  o
 /|\
o | o
 \|/
  o

  o
 / \
o---o
|   |
o---o

o---o
|  /|
| o |
|/  |
o---o

  o
  |
  o
  |
  o
 / \
o---o

o---o
| \ |
o---o o

Based on: http://www.graphclasses.org/smallgraphs.html


Question:
Queue-based topological sort. Develop a topological sort implementation that maintains a vertex-indexed array that keeps track of the indegree of each vertex. Initialize the array and a queue of sources in a single pass through all the edges, as in EXERCISE 4.2.7. Then, perform the following operations until the source queue is empty:
■ Remove a source from the queue and label it.
■ Decrement the entries in the indegree array corresponding to the destination vertex of each of the removed vertex’s edges.
■ If decrementing any entry causes it to become 0, insert the corresponding vertex onto the source queue.

Answer:
4.1.30 - Eulerian and Hamiltonian cycles

An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once.
A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visited twice).

According to Euler theorems a graph has an Eulerian cycle/circuit if and only if it does not have any vertices of odd degree.

First graph:
0-1 0-2 0-3 1-3 1-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9).
It has a Hamiltonian cycle: 1-4 4-8 8-7 7-6 6-9 9-5 5-2 2-0 0-3 3-1

Second graph:
0-1 0-2 0-3 1-3 0-3 2-5 5-6 3-6 4-7 4-8 5-8 5-9 6-7 6-9 8-8

It has an Eulerian cycle (all the vertices have even degrees):
0-3 3-0 0-2 2-5 5-9 9-6 6-5 5-8 8-8 8-4 4-7 7-6 6-3 3-1 1-0
There is no Hamiltonian cycle.

Third graph:
0-1 1-2 1-3 0-3 0-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 4-8 8-7 7-6 6-9 9-5 5-2 2-1 1-3 3-0 0-4

Fourth graph:
4-1 7-9 6-2 7-3 5-0 0-2 0-8 1-6 3-9 6-3 2-8 1-5 9-8 4-5 4-7

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 0-5 5-4 4-1 1-6 6-3 3-7 7-9 9-8 8-2 2-0


Question: Euclidean digraphs. Modify your solution to EXERCISE 4.1.37 to create an API EuclideanDigraph for graphs whose vertices are points in the plane, so that you can work with graphical representations.

Answer:
4.1.31 - Graph enumeration

If we were considering graphs with no self-loops:

There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect.
          (2)
So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)!
             ((2))
             ( E )
different undirected graphs with V vertices and E edges (and no parallel edges and no self-loops).

Since we are also considering graphs with self-loops, there are (V) * 2^V ways to choose a set {u, v} of two vertices in which
                                                                (2)
vertices may or may not have a self-loop.

So there are ((V) * 2^V) = ((V! / 2! * (V - 2)!) * 2^V)! / E! * (((V! / 2! * (V - 2)!) * 2^V) - E)!
             ((2)      )
             (    E    )
different undirected graphs with V vertices and E edges (and no parallel edges).

Reference:
Handbook of Discrete and Combinatorial Mathematics by Kenneth H. Rosen, page 580
https://math.stackexchange.com/questions/1072726/counting-simple-connected-labeled-graphs-with-n-vertices-and-k-edges
https://math.stackexchange.com/questions/128439/how-to-determine-the-number-of-directed-undirected-graphs


Question: Prove that you can rescale the weights by adding a positive constant to all of them or by multiplying them all by a positive constant without affecting the MST.

Answer:
4.1.1

The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2.
Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide the result by 2.

Example:
o - o
| X |
o — o

The minimum number of edges in a graph with V vertices, none of which are isolated (have degree 0) is V - 1.
Example:
o — o — o


Question: Draw all of the MSTs of the graph depicted at right (all edge weights are equal).

Answer:
4.1.2

adj[]
  0  -> 5 -> 2 -> 6
  1  -> 4 -> 8 -> 11
  2  -> 5 -> 6 -> 0 -> 3
  3  -> 10 -> 6 -> 2
  4  -> 1 -> 8
  5  -> 0 -> 10 -> 2
  6  -> 2 -> 3 -> 0
  7  -> 8 -> 11
  8  -> 1 -> 11 -> 7 -> 4
  9  ->
 10  -> 5 -> 3
 11  -> 8 -> 7 -> 1


Question: Give the MST of the weighted graph obtained by deleting vertex 7 from tinyEWG.txt (see page 604).

Answer:
4.1.6

The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections.

adj[]
  0  -> 1 -> 3 (the original was 0 -> 3 -> 1)
  1  -> 2 -> 0
  2  -> 3 -> 1
  3  -> 0 -> 2


Question: Implement the constructor for EdgeWeightedGraph that reads a graph from the input stream, by suitably modifying the constructor from Graph (see page 526).

Answer:
4.1.9
                marked[]    adj[]
dfs (0)          0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5          5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

 dfs (5)         0 T        0 5 2 6
  check 0        1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

  dfs (10)       0 T        0 5 2 6
   check 5       1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

   dfs (3)       0 T        0 5 2 6
    check 10     1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

    dfs (6)      0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

     dfs (2)     0 T        0 5 2 6
      check 5    1          1 4 8 11
      check 6    2 T        2 5 6 0 3
      check 0    3 T        3 10 6 2
      check 3    4          4 1 8
     2 done      5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done

edgeTo[] tree

           0
          5
        10
       3
      6
     2


Question: Develop an EdgeWeightedGraph implementation for dense graphs that uses an adjacency-matrix (two-dimensional array of weights) representation. Disallow parallel edges.

Answer:
4.1.10

Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph.

Proof by contradiction:
If the graph has a node of degree one, removing it gives a connected graph.
Example: o - o

Otherwise, every path in the graph belongs to a cycle. To see this, start with any path, then notice that the terminal nodes of this path must be connected to other nodes that are not in the path, so we add them to the path to make a new path. Since the graph is connected, continuing this process it is possible to see that in the end all the nodes from the graph will be in the path.

If among all those paths there is no path from which one can remove a node without disconnecting the graph, then all those paths are bridges. In this case, they do not belong to a cycle, which is a contradiction (assuming the graph is finite).

Based on: https://math.stackexchange.com/questions/891325/proof-verification-a-connected-graph-always-has-a-vertex-that-is-not-a-cut-vert


Question: Determine the amount of memory used by EdgeWeightedGraph to represent a graph with V vertices and E edges, using the memory-cost model of SECTION 1.4.

Answer:
4.1.11

Tree represented by edgeTo[] after call to bfs(G, 0):

         0
     5   2   6
  10    3


Question: Suppose that a graph has distinct edge weights. Does its shortest edge have to belong to the MST? Can its longest edge belong to the MST? Does a min-weight edge on every cycle have to belong to the MST? Prove your answer to each question or give a counterexample.

Answer:
4.1.12

When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch.
If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance from the root to vertex v and Dw is the distance from the root to vertex w.


Question: Given an MST for an edge-weighted graph G, suppose that an edge in G that does not disconnect G is deleted. Describe how to find an MST of the new graph in time proportional to E.

Answer:
4.1.14

If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths.
This can be seen in the following graph:

  0 (source)
 / \
1 - 2
|   |
4 - 3

If the edge 0 - 2 is inserted before the edge 0 - 1:
Using a stack, the distance from 0 to 4 will be 3.
Using a queue, the distance from 0 to 4 will be 2.

If the edge 0 - 1 is inserted before the edge 0 - 2:
Using a stack, the distance from 0 to 3 will be 3.
Using a queue, the distance from 0 to 3 will be 2.

Thanks to lemonadeseason (https://github.com/lemonadeseason) for correcting the example in this exercise.
https://github.com/reneargento/algorithms-sedgewick-wayne/issues/24


Question: Suppose that you use a priority-queue implementation that maintains a sorted list. What would be the order of growth of the worst-case running time for Prim’s algorithm and for Kruskal’s algorithm for graphs with V vertices and E edges? When would this method be appropriate, if ever? Defend your answer.

Answer:
4.1.19
                   count            marked[]                       id[]
                           0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)               0     T                            0
 dfs(5)              0     T         T                  0         0
  check 0
  dfs(10)            0     T         T         T        0         0         0
   check 5
   dfs(3)            0     T     T   T         T        0     0   0         0
    check 10
    dfs(6)           0     T     T   T T       T        0     0   0 0       0
     dfs(2)          0     T   T T   T T       T        0   0 0   0 0       0
      check 5
      check 6
      check 0
      check 3
     2 done
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done
dfs(1)               1     T T T T   T T       T        0 1 0 0   0 0       0
 dfs(4)              1     T T T T T T T       T        0 1 0 0 1 0 0       0
  check 1
  dfs(8)             1     T T T T T T T   T   T        0 1 0 0 1 0 0   1   0
   check 1
   dfs(11)           1     T T T T T T T   T   T  T     0 1 0 0 1 0 0   1   0  1
    check 8
    dfs(7)           1     T T T T T T T T T   T  T     0 1 0 0 1 0 0 1 1   0  1
     check 8
     check 11
    7 done
    check 1
   11 done
   check 7
   check 4
  8 done
 4 done
 check 8
 check 11
1 done
dfs(9)               2     T T T T T T T T T T T  T     0 1 0 0 1 0 0 1 1 2 0  1
9 done


Question: True or false: At any point during the execution of Kruskal’s algorithm, each vertex is closer to some vertex in its subtree than to any vertex not in its subtree. Prove your answer.

Answer:
4.1.20
             Has Cycle?             marked[]
                  F        0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                     T
 dfs(5)                    T         T
  check 0         F
  dfs(10)                  T         T         T
   check 5        F
   dfs(3)                  T     T   T         T
    check 10      F
    dfs(6)                 T     T   T T       T
     dfs(2)                T   T T   T T       T
      check 5     T (cycle found here)
      check 6     T
      check 0     T
      check 3     T
     2 done
     check 3      T
     check 0      T
    6 done
    check 2       T
   3 done
  10 done
  check 2         T
 5 done
 check 2          T
 check 6          T
0 done
dfs(1)                     T T T T   T T       T
 dfs(4)                    T T T T T T T       T
  check 1         T
  dfs(8)                   T T T T T T T   T   T
   check 1        T
   dfs(11)                 T T T T T T T   T   T  T
    check 8       T
    dfs(7)                 T T T T T T T T T   T  T
     check 8      T
     check 11     T
    7 done
    check 1       T
   11 done
   check 7        T
   check 4        T
  8 done
 4 done
 check 8          T
 check 11         T
1 done
dfs(9)                     T T T T T T T T T T T  T
9 done

The order of growth of the running time of the Cycle constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] array takes time proportional to V.


Question:
Provide an implementation of edges() for PrimMST (page 622).
Solution:
public Iterable<Edge> edges()
{
 Bag<Edge> mst = new Bag<Edge>();
 for (int v = 1; v < edgeTo.length; v++)
  mst.add(edgeTo[v]);
 return mst;
}

Answer:
4.1.21
               Is 2-colorable?                marked[]                     color[]
                     T               0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                               T                            F F F F F F F F F F F  F
 dfs(5)                              T         T                  F F F F F T F F F F F  F
  check 0            T
  dfs(10)                            T         T         T        F F F F F T F F F F F  F
   check 5           T
   dfs(3)                            T     T   T         T        F F F T F T F F F F F  F
    check 10         T
    dfs(6)                           T     T   T T       T        F F F T F T F F F F F  F
     dfs(2)                          T   T T   T T       T        F F T T F T F F F F F  F
      check 5        F
      check 6        F
      check 0        F
      check 3        F
     2 done
     check 3         F
     check 0         F
    6 done
    check 2          F
   3 done
  10 done
  check 2            F
 5 done
 check 2             F
 check 6             F
0 done
dfs(1)                               T T T T   T T       T        F F T T F T F F F F F  F
 dfs(4)                              T T T T T T T       T        F F T T T T F F F F F  F
  check 1            F
  dfs(8)                             T T T T T T T   T   T        F F T T F T F F F F F  F
   check 1           F
   dfs(11)                           T T T T T T T   T   T  T     F F T T F T F F F F F  T
    check 8          F
    dfs(7)                           T T T T T T T T T   T  T     F F T T F T F F F F F  F
     check 8         F
     check 11        F
    7 done
    check 1          F
   11 done
   check 7           F
   check 4           F
  8 done
 4 done
 check 8             F
 check 11            F
1 done
dfs(9)                               T T T T T T T T T T T  T     F F T T F T F F F F F  F
9 done

The order of growth of the running time of the TwoColor constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] and color[] arrays takes time proportional to V.


Question: Reverse-delete algorithm. Develop an implementation that computes the MST as follows: Start with a graph containing all of the edges. Then repeatedly go through the edges in decreasing order of weight. For each edge, check if deleting that edge will disconnect the graph; if not, delete it. Prove that this algorithm computes the MST. What is the order of growth of the number of edge-weight compares performed by your implementation?

Answer:
4.1.24

Number of connected components: 33
Size of the largest component: 118774
Number of components of size less than 10: 5
Does the largest component contain Kevin Bacon: Yes

Eccentricity, diameter, radius, center and girth:

The strategy for computing the eccentricity, diameter, radius, center and girth of the largest component was the following:
The algorithm required for these exact computations has complexity of O(V * E) (running breadth-first search from all vertices). V is ~= 10^5, which means the algorithm required for these exact computation has complexity ~= 10^10.

In order to reduce the complexity of the computations (and to be able to compute them), domain knowledge was used to compute approximate results.
Based on the Kevin Bacon game, we know that Kevin Bacon has been on several movies and that he is closely connected to most of the actors and actresses in the graph. Therefore, he has a high probability of being the center of the graph.

Using Kevin Bacon as the center, we compute his vertex eccentricity to get the graph radius.
A breadth-first search using Kevin Bacon as the source computes the vertices that are furthest from the center. Computing the eccentricities of these vertices we can find the diameter of the graph.

The eccentricities of the center and of the vertices furthest from it are shown in the results. The range of the eccentricities  is [10, 18]. Computing the eccentricities of all vertices would bring us back to the original problem of =~ 10^10 operations.

Finally, for the girth, we know that there is a very high probability that two actors have worked together on two different movies. This gives a girth of 4, which is the minimum girth possible for the movies graph:
Actor -- Movie -- Actor
    \            /
     \   Movie  /
To validate this theory we run the algorithm to compute the girth of the graph but stop once we find a cycle of length 4, since it is the shortest cycle possible.

Eccentricities of Kevin Bacon and of vertices furthest from the center in the largest component:
Eccentricity of vertex 22970: 16
Eccentricity of vertex 22971: 16
Eccentricity of vertex 22972: 16
Eccentricity of vertex 22973: 16
Eccentricity of vertex 22974: 16
Eccentricity of vertex 22976: 16
Eccentricity of vertex 22977: 16
Eccentricity of vertex 22978: 16
Eccentricity of vertex 22979: 16
Eccentricity of vertex 22980: 16
Eccentricity of vertex 51437: 18
Eccentricity of vertex 51438: 18
Eccentricity of vertex 51439: 18
Eccentricity of vertex 51440: 18
Eccentricity of vertex 51441: 18
Eccentricity of vertex 51442: 18
Eccentricity of vertex 51443: 18
Eccentricity of vertex 51444: 18
Eccentricity of vertex 51445: 18
Eccentricity of vertex 51446: 18
Eccentricity of vertex 51447: 18
Eccentricity of vertex 51448: 18
Eccentricity of vertex 51449: 18
Eccentricity of vertex 51450: 18
Eccentricity of vertex 51451: 18
Eccentricity of vertex 51452: 18
Eccentricity of vertex 51453: 18
Eccentricity of vertex 51454: 18
Eccentricity of vertex 51455: 18
Eccentricity of vertex 51456: 18
Eccentricity of vertex 51457: 18
Eccentricity of vertex 51458: 18
Eccentricity of vertex 51459: 18
Eccentricity of vertex 51460: 18
Eccentricity of vertex 51461: 18
Eccentricity of vertex 51462: 18
Eccentricity of vertex 51463: 18
Eccentricity of vertex 51464: 18
Eccentricity of vertex 51465: 18
Eccentricity of vertex 51466: 18
Eccentricity of vertex 51467: 18
Eccentricity of vertex 51468: 18
Eccentricity of vertex 51469: 18
Eccentricity of vertex 51470: 18
Eccentricity of vertex 51471: 18
Eccentricity of vertex 51472: 18
Eccentricity of vertex 51473: 18
Eccentricity of vertex 51474: 18
Eccentricity of vertex 51475: 18
Eccentricity of vertex 51476: 18
Eccentricity of vertex 51477: 18
Eccentricity of vertex 51478: 18
Eccentricity of vertex 51479: 18
Eccentricity of vertex 51480: 18
Eccentricity of vertex 51481: 18
Eccentricity of vertex 51482: 18
Eccentricity of vertex 51483: 18
Eccentricity of vertex 51484: 18
Eccentricity of vertex 51485: 18
Eccentricity of vertex 51486: 18
Eccentricity of vertex 51487: 18
Eccentricity of vertex 51488: 18
Eccentricity of vertex 51489: 18
Eccentricity of vertex 51490: 18
Eccentricity of vertex 51491: 18
Eccentricity of vertex 51492: 18
Eccentricity of vertex 51493: 18
Eccentricity of vertex 51494: 18
Eccentricity of vertex 51495: 18
Eccentricity of vertex 51496: 18
Eccentricity of vertex 51497: 18
Eccentricity of vertex 51498: 18
Eccentricity of vertex 51499: 18
Eccentricity of vertex 51500: 18
Eccentricity of vertex 51501: 18
Eccentricity of vertex 51502: 18
Eccentricity of vertex 51503: 18
Eccentricity of vertex 51504: 18
Eccentricity of vertex 51505: 18
Eccentricity of vertex 51506: 18
Eccentricity of vertex 51507: 18
Eccentricity of vertex 51508: 18
Eccentricity of vertex 51509: 18
Eccentricity of vertex 51510: 18
Eccentricity of vertex 51511: 18
Eccentricity of vertex 51512: 18
Eccentricity of vertex 51513: 18
Eccentricity of vertex 51514: 18
Eccentricity of vertex 51515: 18
Eccentricity of vertex 51516: 18
Eccentricity of vertex 51517: 18
Eccentricity of vertex 51518: 18
Eccentricity of vertex 51519: 18
Eccentricity of vertex 51520: 18
Eccentricity of vertex 86241: 16
Eccentricity of vertex 86242: 16
Eccentricity of vertex 86243: 16
Eccentricity of vertex 86244: 16
Eccentricity of vertex 86245: 16
Eccentricity of vertex 86246: 16
Eccentricity of vertex 86247: 16
Eccentricity of vertex 86248: 16
Eccentricity of vertex 86249: 16
Eccentricity of vertex 86250: 16
Eccentricity of vertex 86251: 16
Eccentricity of vertex 86252: 16
Eccentricity of vertex 86253: 16
Eccentricity of vertex 86254: 16
Eccentricity of vertex 86255: 16
Eccentricity of vertex 86256: 16
Eccentricity of vertex 86257: 16
Eccentricity of vertex 86258: 16
Eccentricity of vertex 86259: 16
Eccentricity of vertex 118353: 18
Eccentricity of vertex 118354: 18
Eccentricity of vertex 118355: 18
Eccentricity of vertex 118356: 18
Eccentricity of vertex 118357: 18
Eccentricity of vertex 118358: 18
Eccentricity of vertex 118359: 18
Eccentricity of vertex 118360: 18
Eccentricity of vertex 118361: 18
Eccentricity of vertex 118362: 18
Eccentricity of vertex 118363: 18
Eccentricity of vertex 118364: 18
Eccentricity of vertex 9145: 10

Diameter of largest component: 18
Radius of largest component: 10
Center of largest component: 9145
Girth of largest component: 4


Question: Animations. Write a client program that does dynamic graphical animations of MST algorithms. Run your program for mediumEWG.txt to produce images like the figures on page 621 and page 624.

Answer:
4.1.27

Integer
* object overhead -> 16 bytes
* int value -> 4 bytes
* padding -> 4 bytes
Amount of memory needed: 16 + 4 + 4 = 24 bytes

Node
* object overhead -> 16 bytes
* extra overhead for reference to the enclosing instance -> 8 bytes
* Item reference (item) -> 8 bytes
* Node reference (next) -> 8 bytes
Amount of memory needed: 16 + 8 + 8 + 8 = 40 bytes

Bag
* object overhead -> 16 bytes
* Node reference (first) -> 8 bytes
* int value (size) -> 4 bytes
* padding -> 4 bytes
* N Nodes -> 40N bytes
* Integer (item) -> 24N bytes
Amount of memory needed: 16 + 8 + 4 + 4 + 40N + 24N = 64N + 32 bytes

Graph
* object overhead -> 16 bytes
* int value (V) -> 4 bytes
* int value (E) -> 4 bytes
* Bag<Integer>[] reference (adj) -> 8 bytes
* Bag<Integer>[] (adj)
   object overhead -> 16 bytes
   int value (length) -> 4 bytes
   padding -> 4 bytes
   Bag references -> 8V
   Bag -> 64E + 32 bytes -> There are V Bags and in total, they have 2E nodes -> 128E + 32V
Amount of memory needed: 16 + 4 + 4 + 8 + 16 + 4 + 4 + 8V + 128E + 32V = 128E + 40V + 56 bytes


Question: Space-efficient data structures. Develop an implementation of the lazy version of Prim’s algorithm that saves space by using lower-level data structures for EdgeWeightedGraph and for MinPQ instead of Bag and Edge. Estimate the amount of memory saved as a function of V and E, using the memory-cost model of SECTION 1.4 (see EXERCISE 4.3.11).

Answer:
4.1.28

Non-isomorphic graphs:

There are 2 non-isomorphic graphs with 2 vertices:
o o

o-o

There are 4 non-isomorphic graphs with 3 vertices:
 o
o o

 o
o-o

o-o-o

 o
/ \
o-o

There are 11 non-isomorphic graphs with 4 vertices:
o o o o

o-o o o

o-o-o o

o-o-o-o

o-o o-o

  o
  |
  o
 / \
o   o

  o
 / \
o---o o

o-o
| |
o-o

  o
  |
  o
 / \
o---o

  o
 / \
o---o
 \ /
  o

   o
  /|\
 / o \
/ / \ \
o------o

There are 34 non-isomorphic graphs with 5 vertices:
  o
o   o
 o o

  o
o   o
 o-o

  o
o    o
    /
 o-o

   o
o     o
 \   /
  o o

   o
 / | \
o  o  o
     o

  o
 / \
o   o
 o-o

   o
 /  \
o    o
 \
  o o

   o
 /  \
o----o
  o o

   o
 /|\\
o o oo

o-o
| |
o-o o

o
|
o-o
| |
o o

  o
  |
  o
 / \
o---o o

o-o-o-o-o

  o  o
 /|  |
o |  |
 \|  |
  o  o

o--o
|  |
o--o
|
o

o   o
|   |
o---o
 \ /
  o

 o--o
  \/
o--o--o

   o
 /  \
o    o
 \   /
  o-o

    o   (This is a complete graph, where all vertices have degree = 4)
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o---o

    o
 / / \\
o------o
\ /\ /\/
 \|/\ |/
  o   o

o---o
|\ /|\
| X | o
|/ \|/
o---o

o---o
|\ /|
| o |
|/ \|
o---o

o---o
|\ /|\
| X | o
|/ \|
o---o

   o
 // \
o-o  o
   \/
    o

o-o-o-o
 \| | /
   o

    o
   /\
  /  \
 /    \
o------o
\  \ / /
 \ /\ /
  o  o

o---o
| X | o
o---o

o   o
|\ /|
| o |
|/ \|
o   o

     o
    /|\
o--o | o
    \|/
     o

  o
  |
  o
 /|\
o | o
 \|/
  o

  o
 / \
o---o
|   |
o---o

o---o
|  /|
| o |
|/  |
o---o

  o
  |
  o
  |
  o
 / \
o---o

o---o
| \ |
o---o o

Based on: http://www.graphclasses.org/smallgraphs.html


Question: Euclidean weighted graphs. Modify your solution to EXERCISE 4.1.37 to create an API EuclideanEdgeWeightedGraph for graphs whose vertices are points in the plane, so that you can work with graphical representations.

Answer:
4.1.30 - Eulerian and Hamiltonian cycles

An Eulerian cycle (or Eulerian circuit) is a path which starts and ends at the same vertex and includes every edge exactly once.
A Hamiltonian cycle is a path which starts and ends at the same vertex and includes every vertex exactly once (except for the source, which is visited twice).

According to Euler theorems a graph has an Eulerian cycle/circuit if and only if it does not have any vertices of odd degree.

First graph:
0-1 0-2 0-3 1-3 1-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9).
It has a Hamiltonian cycle: 1-4 4-8 8-7 7-6 6-9 9-5 5-2 2-0 0-3 3-1

Second graph:
0-1 0-2 0-3 1-3 0-3 2-5 5-6 3-6 4-7 4-8 5-8 5-9 6-7 6-9 8-8

It has an Eulerian cycle (all the vertices have even degrees):
0-3 3-0 0-2 2-5 5-9 9-6 6-5 5-8 8-8 8-4 4-7 7-6 6-3 3-1 1-0
There is no Hamiltonian cycle.

Third graph:
0-1 1-2 1-3 0-3 0-4 2-5 2-9 3-6 4-7 4-8 5-8 5-9 6-7 6-9 7-8

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 4-8 8-7 7-6 6-9 9-5 5-2 2-1 1-3 3-0 0-4

Fourth graph:
4-1 7-9 6-2 7-3 5-0 0-2 0-8 1-6 3-9 6-3 2-8 1-5 9-8 4-5 4-7

It does not have an Eulerian cycle because it has vertices of odd degree (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
It has a Hamiltonian cycle: 0-5 5-4 4-1 1-6 6-3 3-7 7-9 9-8 8-2 2-0


Question: MST weights. Develop implementations of weight() for LazyPrimMST, PrimMST, and KruskalMST, using a lazy strategy that iterates through the MST edges when the client calls weight().Then develop alternate implementations that use an eager strategy that maintains a running total as the MST is computed.

Answer:
4.1.31 - Graph enumeration

If we were considering graphs with no self-loops:

There are (V) ways to choose a set {u, v} of two vertices. From this set there are E ways to choose the vertices to connect.
          (2)
So there are ((V)) = (V! / 2! * (V - 2)!)! / E! * ((V! / 2! * (V - 2)!) - E)!
             ((2))
             ( E )
different undirected graphs with V vertices and E edges (and no parallel edges and no self-loops).

Since we are also considering graphs with self-loops, there are (V) * 2^V ways to choose a set {u, v} of two vertices in which
                                                                (2)
vertices may or may not have a self-loop.

So there are ((V) * 2^V) = ((V! / 2! * (V - 2)!) * 2^V)! / E! * (((V! / 2! * (V - 2)!) * 2^V) - E)!
             ((2)      )
             (    E    )
different undirected graphs with V vertices and E edges (and no parallel edges).

Reference:
Handbook of Discrete and Combinatorial Mathematics by Kenneth H. Rosen, page 580
https://math.stackexchange.com/questions/1072726/counting-simple-connected-labeled-graphs-with-n-vertices-and-k-edges
https://math.stackexchange.com/questions/128439/how-to-determine-the-number-of-directed-undirected-graphs


Question: Certification. Write an MST and EdgeWeightedGraph client check() that uses the following cut optimality conditions implied by PROPOSITION J to verify that a proposed set of edges is in fact an MST: A set of edges is an MST if it is a spanning tree and every edge is a minimum-weight edge in the cut defined by removing that edge from the tree. What is the order of growth of the running time of your method?

Answer:
4.1.33 - Odd cycles

A graph is two-colorable (bipartite) if and only if it contains no odd-length cycle.

Proof:
1- Proving that a graph with an odd-length cycle cannot be bipartite:
If a graph G is bipartite with vertex sets V1 and V2, every step along a walk takes you either from V1 to V2 or from V2 to V1. To end up where you started, therefore, you must take an even number of steps.

2- Proving that a graph with only even-length cycles is bipartite:
Consider G to be a graph with only even-length cycles. Let v0 be any vertex. For each vertex v in the same component C0 as v0 let d(v) be the length of the shortest path from v0 to v. Color red every vertex in C0 whose distance from v0 is even, and color the other vertices of C0 blue. Do the same for each component of G. Check that if G had any edge between two red vertices or between two blue vertices, it would have an odd cycle. Thus, G is bipartite, the red vertices and the blue vertices being the two parts.

Reference:
https://math.stackexchange.com/questions/311665/proof-a-graph-is-bipartite-if-and-only-if-it-contains-no-odd-cycles


Question: True or false: Adding a constant to every edge weight does not change the solution to the single-source shortest-paths problem.

Answer:
4.1.1

The maximum number of edges in a graph with V vertices and no parallel edges is V * (V - 1) / 2.
Since we do not have self-loops or parallel edges, each vertex can connect to V - 1 other vertices. In an undirected graph vertex v connected to vertex w is the same as vertex w connected to vertex v, so we divide the result by 2.

Example:
o - o
| X |
o — o

The minimum number of edges in a graph with V vertices, none of which are isolated (have degree 0) is V - 1.
Example:
o — o — o


Question: Provide an implementation of toString() for EdgeWeightedDigraph.

Answer:
4.1.2

adj[]
  0  -> 5 -> 2 -> 6
  1  -> 4 -> 8 -> 11
  2  -> 5 -> 6 -> 0 -> 3
  3  -> 10 -> 6 -> 2
  4  -> 1 -> 8
  5  -> 0 -> 10 -> 2
  6  -> 2 -> 3 -> 0
  7  -> 8 -> 11
  8  -> 1 -> 11 -> 7 -> 4
  9  ->
 10  -> 5 -> 3
 11  -> 8 -> 7 -> 1


Question: Give a trace that shows the process of computing the SPT of the digraph defined in EXERCISE 4.4.5 with the eager version of Dijkstra’s algorithm.

Answer:
4.1.6

The edges form a cycle, so changing the connection order of one of the vertices' adjacency list creates an impossible sequence of connections.

adj[]
  0  -> 1 -> 3 (the original was 0 -> 3 -> 1)
  1  -> 2 -> 0
  2  -> 3 -> 1
  3  -> 0 -> 2


Question: The table below, from an old published road map, purports to give the length of the shortest routes connecting the cities. It contains an error. Correct the table. Also, add a table that shows how to achieve the shortest routes.

Answer:
4.1.9
                marked[]    adj[]
dfs (0)          0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5          5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

 dfs (5)         0 T        0 5 2 6
  check 0        1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10         10 5 3
                11         11 8 7 1

  dfs (10)       0 T        0 5 2 6
   check 5       1          1 4 8 11
                 2          2 5 6 0 3
                 3          3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

   dfs (3)       0 T        0 5 2 6
    check 10     1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6          6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

    dfs (6)      0 T        0 5 2 6
                 1          1 4 8 11
                 2          2 5 6 0 3
                 3 T        3 10 6 2
                 4          4 1 8
                 5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1

     dfs (2)     0 T        0 5 2 6
      check 5    1          1 4 8 11
      check 6    2 T        2 5 6 0 3
      check 0    3 T        3 10 6 2
      check 3    4          4 1 8
     2 done      5 T        5 0 10 2
                 6 T        6 2 3 0
                 7          7 8 11
                 8          8 1 11 7 4
                 9          9
                10 T       10 5 3
                11         11 8 7 1
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done

edgeTo[] tree

           0
          5
        10
       3
      6
     2


Question: Consider the edges in the digraph defined in EXERCISE 4.4.4 to be undirected edges such that each edge corresponds to equal-weight edges in both directions in the edge-weighted digraph. Answer EXERCISE 4.4.6 for this corresponding edge-weighted digraph.

Answer:
4.1.10

Every connected graph has a vertex whose removal (including all incident edges) will not disconnect the graph.

Proof by contradiction:
If the graph has a node of degree one, removing it gives a connected graph.
Example: o - o

Otherwise, every path in the graph belongs to a cycle. To see this, start with any path, then notice that the terminal nodes of this path must be connected to other nodes that are not in the path, so we add them to the path to make a new path. Since the graph is connected, continuing this process it is possible to see that in the end all the nodes from the graph will be in the path.

If among all those paths there is no path from which one can remove a node without disconnecting the graph, then all those paths are bridges. In this case, they do not belong to a cycle, which is a contradiction (assuming the graph is finite).

Based on: https://math.stackexchange.com/questions/891325/proof-verification-a-connected-graph-always-has-a-vertex-that-is-not-a-cut-vert


Question: Use the memory-cost model of SECTION 1.4 to determine the amount of memory used by EdgeWeightedDigraph to represent a graph with V vertices and E edges.

Answer:
4.1.11

Tree represented by edgeTo[] after call to bfs(G, 0):

         0
     5   2   6
  10    3


Question: Adapt the DirectedCycle and Topological classes from SECTION 4.2 to use the EdgeWeightedDigraph and DirectedEdge APIs of this section, thus implementing EdgeWeightedCycleFinder and EdgeWeightedTopological classes.

Answer:
4.1.12

When neither v nor w are at the root, the BFS tree tell us that if they are on the same branch, there is a path between v and w of distance equal to the number of edges between them in the branch.
If they are not on the same branch, there is a path between v and w of distance Dv + Dw, where Dv is the distance from the root to vertex v and Dw is the distance from the root to vertex w.


Question: Show the paths that would be discovered by the two strawman approaches described on page 668 for the example tinyEWDn.txt shown on that page.

Answer:
4.1.14

If we use a stack instead of a queue when running breadth-first search, it may not compute shortest paths.
This can be seen in the following graph:

  0 (source)
 / \
1 - 2
|   |
4 - 3

If the edge 0 - 2 is inserted before the edge 0 - 1:
Using a stack, the distance from 0 to 4 will be 3.
Using a queue, the distance from 0 to 4 will be 2.

If the edge 0 - 1 is inserted before the edge 0 - 2:
Using a stack, the distance from 0 to 3 will be 3.
Using a queue, the distance from 0 to 3 will be 2.

Thanks to lemonadeseason (https://github.com/lemonadeseason) for correcting the example in this exercise.
https://github.com/reneargento/algorithms-sedgewick-wayne/issues/24


Question: Find the lowest-weight cycle (best arbitrage opportunity) in the example shown in the text.

Answer:
4.1.19
                   count            marked[]                       id[]
                           0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)               0     T                            0
 dfs(5)              0     T         T                  0         0
  check 0
  dfs(10)            0     T         T         T        0         0         0
   check 5
   dfs(3)            0     T     T   T         T        0     0   0         0
    check 10
    dfs(6)           0     T     T   T T       T        0     0   0 0       0
     dfs(2)          0     T   T T   T T       T        0   0 0   0 0       0
      check 5
      check 6
      check 0
      check 3
     2 done
     check 3
     check 0
    6 done
    check 2
   3 done
  10 done
  check 2
 5 done
 check 2
 check 6
0 done
dfs(1)               1     T T T T   T T       T        0 1 0 0   0 0       0
 dfs(4)              1     T T T T T T T       T        0 1 0 0 1 0 0       0
  check 1
  dfs(8)             1     T T T T T T T   T   T        0 1 0 0 1 0 0   1   0
   check 1
   dfs(11)           1     T T T T T T T   T   T  T     0 1 0 0 1 0 0   1   0  1
    check 8
    dfs(7)           1     T T T T T T T T T   T  T     0 1 0 0 1 0 0 1 1   0  1
     check 8
     check 11
    7 done
    check 1
   11 done
   check 7
   check 4
  8 done
 4 done
 check 8
 check 11
1 done
dfs(9)               2     T T T T T T T T T T T  T     0 1 0 0 1 0 0 1 1 2 0  1
9 done


Question: Find a currency-conversion table online or in a newspaper. Use it to build an arbitrage table. Note: Avoid tables that are derived (calculated) from a few values and that therefore do not give sufficiently accurate conversion information to be interesting. Extra credit: Make a killing in the money-exchange market!

Answer:
4.1.20
             Has Cycle?             marked[]
                  F        0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                     T
 dfs(5)                    T         T
  check 0         F
  dfs(10)                  T         T         T
   check 5        F
   dfs(3)                  T     T   T         T
    check 10      F
    dfs(6)                 T     T   T T       T
     dfs(2)                T   T T   T T       T
      check 5     T (cycle found here)
      check 6     T
      check 0     T
      check 3     T
     2 done
     check 3      T
     check 0      T
    6 done
    check 2       T
   3 done
  10 done
  check 2         T
 5 done
 check 2          T
 check 6          T
0 done
dfs(1)                     T T T T   T T       T
 dfs(4)                    T T T T T T T       T
  check 1         T
  dfs(8)                   T T T T T T T   T   T
   check 1        T
   dfs(11)                 T T T T T T T   T   T  T
    check 8       T
    dfs(7)                 T T T T T T T T T   T  T
     check 8      T
     check 11     T
    7 done
    check 1       T
   11 done
   check 7        T
   check 4        T
  8 done
 4 done
 check 8          T
 check 11         T
1 done
dfs(9)                     T T T T T T T T T T T  T
9 done

The order of growth of the running time of the Cycle constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] array takes time proportional to V.


Question: Show, in the style of the trace in the text, the process of computing the SPT with the Bellman-Ford algorithm for the edge-weighted digraph of EXERCISE 4.4.5.

Answer:
4.1.21
               Is 2-colorable?                marked[]                     color[]
                     T               0 1 2 3 4 5 6 7 8 9 10 11    0 1 2 3 4 5 6 7 8 9 10 11
dfs(0)                               T                            F F F F F F F F F F F  F
 dfs(5)                              T         T                  F F F F F T F F F F F  F
  check 0            T
  dfs(10)                            T         T         T        F F F F F T F F F F F  F
   check 5           T
   dfs(3)                            T     T   T         T        F F F T F T F F F F F  F
    check 10         T
    dfs(6)                           T     T   T T       T        F F F T F T F F F F F  F
     dfs(2)                          T   T T   T T       T        F F T T F T F F F F F  F
      check 5        F
      check 6        F
      check 0        F
      check 3        F
     2 done
     check 3         F
     check 0         F
    6 done
    check 2          F
   3 done
  10 done
  check 2            F
 5 done
 check 2             F
 check 6             F
0 done
dfs(1)                               T T T T   T T       T        F F T T F T F F F F F  F
 dfs(4)                              T T T T T T T       T        F F T T T T F F F F F  F
  check 1            F
  dfs(8)                             T T T T T T T   T   T        F F T T F T F F F F F  F
   check 1           F
   dfs(11)                           T T T T T T T   T   T  T     F F T T F T F F F F F  T
    check 8          F
    dfs(7)                           T T T T T T T T T   T  T     F F T T F T F F F F F  F
     check 8         F
     check 11        F
    7 done
    check 1          F
   11 done
   check 7           F
   check 4           F
  8 done
 4 done
 check 8             F
 check 11            F
1 done
dfs(9)                               T T T T T T T T T T T  T     F F T T F T F F F F F  F
9 done

The order of growth of the running time of the TwoColor constructor, in the worst case is O(V + E).
Each adjacency-list entry is examined exactly once, and there are 2 * E such entries (two for each edge); initializing the marked[] and color[] arrays takes time proportional to V.


Question: Multisource shortest paths. Develop an API and implementation that uses Dijkstra’s algorithm to solve the multisource shortest-paths problem on edge-weighted digraphs with positive edge weights: given a set of sources, find a shortest-paths forest that enables implementation of a method that returns to clients the shortest path from any source to each vertex. Hint: Add a dummy vertex with a zero-weight edge to each source, or initialize the priority queue with all sources, with their distTo[] entries set to 0.

Answer:
4.1.24

Number of connected components: 33
Size of the largest component: 118774
Number of components of size less than 10: 5
Does the largest component contain Kevin Bacon: Yes

Eccentricity, diameter, radius, center and girth:

The strategy for computing the eccentricity, diameter, radius, center and girth of the largest component was the following:
The algorithm required for these exact computations has complexity of O(V * E) (running breadth-first search from all vertices). V is ~= 10^5, which means the algorithm required for these exact computation has complexity ~= 10^10.

In order to reduce the complexity of the computations (and to be able to compute them), domain knowledge was used to compute approximate results.
Based on the Kevin Bacon game, we know that Kevin Bacon has been on several movies and that he is closely connected to most of the actors and actresses in the graph. Therefore, he has a high probability of being the center of the graph.

Using Kevin Bacon as the center, we compute his vertex eccentricity to get the graph radius.
A breadth-first search using Kevin Bacon as the source computes the vertices that are furthest from the center. Computing the eccentricities of these vertices we can find the diameter of the graph.

The eccentricities of the center and of the vertices furthest from it are shown in the results. The range of the eccentricities  is [10, 18]. Computing the eccentricities of all vertices would bring us back to the original problem of =~ 10^10 operations.

Finally, for the girth, we know that there is a very high probability that two actors have worked together on two different movies. This gives a girth of 4, which is the minimum girth possible for the movies graph:
Actor -- Movie -- Actor
    \            /
     \   Movie  /
To validate this theory we run the algorithm to compute the girth of the graph but stop once we find a cycle of length 4, since it is the shortest cycle possible.

Eccentricities of Kevin Bacon and of vertices furthest from the center in the largest component:
Eccentricity of vertex 22970: 16
Eccentricity of vertex 22971: 16
Eccentricity of vertex 22972: 16
Eccentricity of vertex 22973: 16
Eccentricity of vertex 22974: 16
Eccentricity of vertex 22976: 16
Eccentricity of vertex 22977: 16
Eccentricity of vertex 22978: 16
Eccentricity of vertex 22979: 16
Eccentricity of vertex 22980: 16
Eccentricity of vertex 51437: 18
Eccentricity of vertex 51438: 18
Eccentricity of vertex 51439: 18
Eccentricity of vertex 51440: 18
Eccentricity of vertex 51441: 18
Eccentricity of vertex 51442: 18
Eccentricity of vertex 51443: 18
Eccentricity of vertex 51444: 18
Eccentricity of vertex 51445: 18
Eccentricity of vertex 51446: 18
Eccentricity of vertex 51447: 18
Eccentricity of vertex 51448: 18
Eccentricity of vertex 51449: 18
Eccentricity of vertex 51450: 18
Eccentricity of vertex 51451: 18
Eccentricity of vertex 51452: 18
Eccentricity of vertex 51453: 18
Eccentricity of vertex 51454: 18
Eccentricity of vertex 51455: 18
Eccentricity of vertex 51456: 18
Eccentricity of vertex 51457: 18
Eccentricity of vertex 51458: 18
Eccentricity of vertex 51459: 18
Eccentricity of vertex 51460: 18
Eccentricity of vertex 51461: 18
Eccentricity of vertex 51462: 18
Eccentricity of vertex 51463: 18
Eccentricity of vertex 51464: 18
Eccentricity of vertex 51465: 18
Eccentricity of vertex 51466: 18
Eccentricity of vertex 51467: 18
Eccentricity of vertex 51468: 18
Eccentricity of vertex 51469: 18
Eccentricity of vertex 51470: 18
Eccentricity of vertex 51471: 18
Eccentricity of vertex 51472: 18
Eccentricity of vertex 51473: 18
Eccentricity of vertex 51474: 18
Eccentricity of vertex 51475: 18
Eccentricity of vertex 51476: 18
Eccentricity of vertex 51477: 18
Eccentricity of vertex 51478: 18
Eccentricity of vertex 51479: 18
Eccentricity of vertex 51480: 18
Eccentricity of vertex 51481: 18
Eccentricity of vertex 51482: 18
Eccentricity of vertex 51483: 18
Eccentricity of vertex 51484: 18
Eccentricity of vertex 51485: 18
Eccentricity of vertex 51486: 18
Eccentricity of vertex 51487: 18
Eccentricity of vertex 51488: 18
Eccentricity of vertex 51489: 18
Eccentricity of vertex 51490: 18
Eccentricity of vertex 51491: 18
Eccentricity of vertex 51492: 18
Eccentricity of vertex 51493: 18
Eccentricity of vertex 51494: 18
Eccentricity of vertex 51495: 18
Eccentricity of vertex 51496: 18
Eccentricity of vertex 51497: 18
Eccentricity of vertex 51498: 18
Eccentricity of vertex 51499: 18
Eccentricity of vertex 51500: 18
Eccentricity of vertex 51501: 18
Eccentricity of vertex 51502: 18
Eccentricity of vertex 51503: 18
Eccentricity of vertex 51504: 18
Eccentricity of vertex 51505: 18
Eccentricity of vertex 51506: 18
Eccentricity of vertex 51507: 18
Eccentricity of vertex 51508: 18
Eccentricity of vertex 51509: 18
Eccentricity of vertex 51510: 18
Eccentricity of vertex 51511: 18
Eccentricity of vertex 51512: 18
Eccentricity of vertex 51513: 18
Eccentricity of vertex 51514: 18
Eccentricity of vertex 51515: 18
Eccentricity of vertex 51516: 18
Eccentricity of vertex 51517: 18
Eccentricity of vertex 51518: 18
Eccentricity of vertex 51519: 18
Eccentricity of vertex 51520: 18
Eccentricity of vertex 86241: 16
Eccentricity of vertex 86242: 16
Eccentricity of vertex 86243: 16
Eccentricity of vertex 86244: 16
Eccentricity of vertex 86245: 16
Eccentricity of vertex 86246: 16
Eccentricity of vertex 86247: 16
Eccentricity of vertex 86248: 16
Eccentricity of vertex 86249: 16
Eccentricity of vertex 86250: 16
Eccentricity of vertex 86251: 16
Eccentricity of vertex 86252: 16
Eccentricity of vertex 86253: 16
Eccentricity of vertex 86254: 16
Eccentricity of vertex 86255: 16
Eccentricity of vertex 86256: 16
Eccentricity of vertex 86257: 16
Eccentricity of vertex 86258: 16
Eccentricity of vertex 86259: 16
Eccentricity of vertex 118353: 18
Eccentricity of vertex 118354: 18
Eccentricity of vertex 118355: 18
Eccentricity of vertex 118356: 18
Eccentricity of vertex 118357: 18
Eccentricity of vertex 118358: 18
Eccentricity of vertex 118359: 18
Eccentricity of vertex 118360: 18
Eccentricity of vertex 118361: 18
Eccentricity of vertex 118362: 18
Eccentricity of vertex 118363: 18
Eccentricity of vertex 118364: 18
Eccentricity of vertex 9145: 10

Diameter of largest component: 18
Radius of largest component: 10
Center of largest component: 9145
Girth of largest component: 4