【Data Structure and Algorithm】Graph Exercises

topic

There are a total of seven post-school exercises related to graphs below, all of which are ideas drawing questions and not algorithm design questions, so I will list them together here~

1. Given the directed graph shown in the figure below, please answer the following questions

insert image description here

  1. the in/out degree of each vertex;
  2. adjacency matrix;
  3. adjacency list;
  4. inverse adjacency list;
  5. strongly connected components

answer

in/out degree of each vertex

(The following will use ①+ to indicate the in-degree of ① vertex, ①- to represent the out-degree, and other vertices by analogy)

Vertex out degree Vertex in-degree
①+:3 ①-:0
②+:2 ②-:2
③+:1 ③-:2
④+:1 ④-:3
⑤+:2 ⑤-:1
⑥+:2 ⑥-:3

adjacency matrix

insert image description here

adjacency list

insert image description here

inverse adjacency list

insert image description here

strongly connected components

insert image description here


2. The adjacency matrix of the graph represented by the known two-dimensional array is shown in the figure below, and the depth-first spanning tree and the breadth-first spanning tree obtained by traversing from vertex 1 are respectively drawn.

insert image description here


answer

The depth-first spanning tree obtained by traversing from vertex 1

insert image description here

The breadth-first spanning tree obtained by traversing from vertex 1

insert image description here


3. Answer the following two questions for the following picture

insert image description here

  1. Write out its adjacency matrix, and find its minimum spanning tree according to Prim's algorithm
  2. Write out its adjacency list, and find its minimum spanning tree according to the Kruskal algorithm

answer

Write out its adjacency matrix, and find its minimum spanning tree according to Prim's algorithm

insert image description here
insert image description here
insert image description here

Write out its adjacency list, and find its minimum spanning tree according to the Kruskal algorithm

insert image description here
insert image description here


4. Answer the following two questions for the following picture

insert image description here

  1. List all possible topologically ordered sequences
  2. Write out its adjacency list, and point out which sequence is obtained by applying the TopLogicalSort algorithm learned in class

answer

List all possible topologically ordered sequences

  • 152634
  • 512634
  • 156234
  • 516234

Write out its adjacency list, and point out which sequence is obtained by applying the TopLogicalSort algorithm learned in class

insert image description here
The sequence obtained using the TopLogicalSort algorithm learned in class is: 563412


5. For the AOE network in the figure below, calculate the relative values ​​of active arcs and vertices

Calculate the value of e ( ai ) , l ( ai ) of each arc, ve ( vi ) of each event (vertex), l ( vi ) Calculate the value of e(a_i), l(a_i) of each arc, each event ( Vertex) ve(v_i), l(v_i)Compute the e ( ai)l(ai) , the value of v e ( vi)l(vi)
insert image description here


answer

The numbers of the individual arcs and vertices are shown in the figure below
insert image description here

V e ( v i ) Ve(v_i) V e ( vi)

  • V e ( A ) = 1 Ve(A) = 1Ve ( A ) _=1
  • V e ( B ) = 6 Ve(B) = 6V e ( B )=6
  • V e ( C ) = 17 ( a → G → H → C ) Ve(C) = 17(a→G→H→C) Ve ( C ) _=17(aGHC)
  • V e ( D ) = 3 Ve(D) = 3Ve ( D ) _=3
  • V e ( E ) = 25 ( a → G → H → C → E ) And(E) = 25(a→G→H→C→E)V e ( E )=25(aGHCE)
  • V e ( F ) = 4 Ve(F) = 4 Ve(F)=4
  • V e ( G ) = 3 Ve(G) = 3 V e ( G )=3
  • V e ( H ) = 13 ( a → G → H ) Ve(H) = 13(a→G→H) Ve(H)=13(aGH)
  • V e ( I ) = 1 Ve(I) = 1 Ve(I)=1
  • V e ( J ) = 31 ( a → G → H → K → J ) Ve(J) = 31(a→G→H→K→J) Ve ( J ) _=31(aGHKJ)
  • V e ( K ) = 22 ( a → G → H → K ) And(K) = 22(a→G→H→K)V e ( N )=22(aGHK)
  • V e ( ∞ ) = 43 Ve(∞) = 43Ve()=43

I ( vi ) I(v_i)I ( vi)

  • I ( A ) = 19 I(A) = 19 I(A)=19
  • I ( B ) = 23 I(B) = 23 I(B)=23
  • I ( C ) = 25 I(C) = 25 I(C)=25
  • I ( D ) = 18 I(D) = 18 I(D)=18
  • I ( E ) = 33 I(E) = 33I ( E )=33
  • I ( F ) = 8 I(F) = 8 I(F)=8
  • I ( G ) = 3 I(G) = 3 I(G)=3
  • I ( H ) = 13 I(H) = 13 I(H)=13
  • I ( I ) = 7 I(I) = 7 I(I)=7
  • I ( J ) = 31 I(J) = 31 I(J)=31
  • I ( K ) = 22 I(K) = 22 I(K)=22
  • I ( ∞ ) = 43 I(∞) = 43 I()=43

e ( a i ) e(a_i) and ( ai)

  • and ( a 1 ) = 0 and (a_1) = 0and ( a1)=0
  • and ( a 2 ) = 0 and (a_2) = 0and ( a2)=0
  • and ( a 3 ) = 0 and (a_3) = 0and ( a3)=0
  • and ( a 4 ) = 0 and (a_4) = 0and ( a4)=0
  • and ( a 5 ) = 0 and (a_5) = 0and ( a5)=0
  • and ( a 6 ) = 0 and (a_6) = 0and ( a6)=0
  • and ( a 8 ) = 6 and (a_8) = 6and ( a8)=6
  • and ( a 9 ) = 3 and (a_9) = 3and ( a9)=3
  • and ( a 10 ) = 3 and (a_{10}) = 3and ( a10)=3
  • and ( a 11 ) = 3 and (a_{11}) = 3and ( a11)=3
  • and ( a 12 ) = 4 and (a_{12}) = 4and ( a12)=4
  • and ( a 13 ) = 4 and (a_{13}) = 4and ( a13)=4
  • and ( a 14 ) = 3 and (a_{14}) = 3and ( a14)=3
  • and ( a 15 ) = 3 and (a_{15}) = 3and ( a15)=3
  • and ( a 16 ) = 1 and (a_{16}) = 1and ( a16)=1
  • and ( a 17 ) = 17 and (a_{17}) = 17and ( a17)=17
  • and ( a 18 ) = 13 and (a_{18}) = 13and ( a18)=13
  • and ( a 19 ) = 13 and (a_{19}) = 13and ( a19)=13
  • and ( a 20 ) = 13 and (a_{20}) = 13and ( a20)=13
  • and ( a 21 ) = 22 and (a_{21}) = 22and ( a21)=22
  • and ( a 22 ) = 31 and (a_{22}) = 31and ( a22)=31
  • and ( a 23 ) = 31 and (a_{23}) = 31and ( a23)=31
  • and ( a 24 ) = 25 and (a_{24}) = 25and ( a24)=25

l ( a i ) l(a_i) l(ai)

  • l ( a 1 ) = 18 l(a_1) = 18 l(a1)=18
  • l ( a 2 ) = 17 l(a_2) = 17 l(a2)=17
  • l ( a 3 ) = 15 l(a_3) = 15 l(a3)=15
  • l ( a 4 ) = 4 l(a_4) = 4 l(a4)=4
  • l ( a 5 ) = 0 l(a_5) = 0 l(a5)=0
  • l ( a 6 ) = 6 l(a_6) = 6 l(a6)=6
  • l ( a 7 ) = 19 l(a_7) = 19 l(a7)=19
  • l ( a 8 ) = 22 l(a_8) = 22 l(a8)=22
  • l ( a 9 ) = 18 l(a_9) = 18 l(a9)=18
  • l ( a 10 ) = 25 l(a_{10}) = 25 l(a10)=25
  • l ( a 11 ) = 25 l(a_{11}) = 25 l(a11)=25
  • l ( a 12 ) = 27 l(a_{12}) = 27 l(a12)=27
  • l ( a 13 ) = 8 l(a_{13}) = 8 l(a13)=8
  • l ( a 14 ) = 22 l(a_{14}) = 22 l(a14)=22
  • l ( a 15 ) = 3 l(a_{15}) = 3 l(a15)=3
  • l ( a 16 ) = 7 l(a_{16}) = 7 l(a16)=7
  • l ( a 17 ) = 25 l(a_{17}) = 25 l(a17)=25
  • l ( a 18 ) = 21 l(a_{18}) = 21 l(a18)=21
  • l ( a 19 ) = 27 l(a_{19}) = 27 l(a19)=27
  • l ( a 20 ) = 13 l(a_{20}) = 13 l(a20)=13
  • l ( a 21 ) = 22 l(a_{21}) = 22 l(a21)=22
  • l ( a 22 ) = 30 l(a_{22}) =30 l(a22)=30
  • l ( a 23 ) = 31 l(a_{23}) = 31 l(a23)=31
  • l ( a 24 ) = 33 l(a_{24}) = 33 l(a24)=33

6. Use Dijkstra algorithm to find the shortest path from vertex a to other vertices in the graph, and write the state of each step in the process of executing the algorithm

insert image description here


answer

step one

Starting with a as the initial pending path table
insert image description here

step two

Select the shortest edge from the pending path table, set its vertex as a new growth point, and compare each of the remaining growth points, if the path that takes the new growth point as a transit is shorter than the original path, replace it.
insert image description here

next steps

Repeat step 2 n-2 times (Note: Green means that there is a new path, but the path length is greater than the original path, and red means that the path length of the new path is smaller than the original path, so it is replaced)
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here


7. Use Floyd's algorithm to find the shortest path between each pair of vertices in the graph

insert image description here

answer

STEP1: Initial adjacency matrix D, and Path

insert image description here

STEP2: Take A as the transit point and perform the first iteration

insert image description here

STEP3: Take B as the transition point, and carry out the second iteration

insert image description here

STEP4: Take C as the transit point for the third iteration

insert image description here

STEP5: Take D as the turning point, and carry out the fourth iteration

insert image description here

STEP6: Get the final D, and Path matrix

insert image description here


conclusion

  Because it is an algorithm side dish, the methods and ideas provided may not be very good, please bear with me~ If you have any questions, please leave a message to discuss. If you think this article is helpful to you, can you give me a free like? The communication between us is my biggest motivation!

Guess you like

Origin blog.csdn.net/Zchengjisihan/article/details/131577940