0

How to write pesoduocode for following graph !

Figure 23.2 http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap23.htm here what I have

// adj-list

for each u ∈ v [G]

do empty list Adj-list[u]

for each u ∈ v [G]

do if (u,v) ∈ E //if there is edge between u,v

then add v to Adj-list [u]

but i don't know how to deal with the directed edge any help please? the second one

//adj-matrix

for i=1 to n

for j=1 to n

if (i,j)∈ E

adj-matrix [i][j]=1

else adj-matrix [i][j]=0

As123
  • 11
  • 1
  • 6
  • Don't worry - you're on the right path. Actually coding this up and running it would likely help. With regards to the directed edge, notice how you only added the edge for u,v into the list of edges for node u, but not node v - so it is directed. If you wanted it to be undirected you'd also add it to node v's list of edges. – J Trana Jul 05 '15 at 01:41

1 Answers1

0

This can be implemented using Map<Node, List<Node>>. Nodes are vertices in your graph that then correlate to a list of Nodes that they are connected to. This model handles the directed adjacency list as if node A is connected to node B in that direction - node B will appear in node A's list, but not vice versa.

skywavex
  • 29
  • 5