DEA in Bioinformatics 2002-2003 Principles of graph theory Jacques van Helden Jacques.van.Helden@ulb.ac.be Proverbios y cantares - XXIX Caminante, son tus huellas el camino y nada más; Caminante, no hay camino, se hace camino al andar. Al andar se hace el camino, y al volver la vista atrás se ve la senda que nunca se ha de volver a pisar. Caminante no hay camino sino estelas en la mar. Antonio Machado Contents Basic concepts of graph theory Applications of graph theory to metabolic networks Definitions Descriptions of a graph Walks, trails and paths Trees Spanning trees Structural properties of a graph Examples of metabolic networks Mapping metabolic networks onto a graph Taversal rules for metabolic graphs Path finding Pathway reconstruction by reaction clustering From gene expression data to pathways Acknowledgements DEA in Bioinformatics 2001 Basic concepts of graph theory Jacques van Helden Jacques.van.Helden@ulb.ac.be Basic concepts of graph theory Graph definitions Jacques van Helden Jacques.van.Helden@ulb.ac.be Graph graph vertex (node) edge self-loop proper edge multi-edge simple graph A graph (G) contains a set of vertices (V) and a set of edges (E) A simple graph contains no selfloop and no multi-edge Directed Graph (= Digraph) digraph vertex (= node) arc (= directed edge) self-loop proper arc multi-arc not a multi-arc ! simple digraph A directed edge (or arc) is characterized by a head and a tail A digraph is a graph whose edges are directed A partially directed graph is a graph combining directed and non-directed edges Basic concepts of graph theory Graph descriptions Jacques van Helden Jacques.van.Helden@ulb.ac.be Graph descriptions : incidence matrix one row per edge one colum per vertex value = 1 if edge and vertex are incident Problems only valid for undirected graphs inefficient storage (many empty cells) edge\vertex e1 e2 e3 e4 e5 e6 e7 e8 a 1 1 0 0 0 0 0 0 b 1 1 1 1 1 0 0 0 c 0 0 1 0 0 1 0 0 d 0 0 0 0 0 0 0 0 e 0 0 0 1 1 1 1 0 f 0 0 0 0 0 0 0 0 g 0 0 0 0 0 0 0 1 h 0 0 0 0 0 0 0 1 e1 a e2 e5 e7 d f b e3 e4 c e6 e g e8 h Graph descriptions : adjacency matrix one row per vertex one colum per vertex value = 1 if vertices are adjacent diagonal = self-loops problems from\to a b c d e f g h no possibility to represent multi-arcs inefficient storage (many empty cells) a 0 0 0 0 0 0 0 0 b 1 0 0 0 1 0 0 0 c 0 1 0 0 1 0 0 0 d 0 0 0 1 0 0 0 0 e 0 1 0 0 0 0 0 0 f 0 0 0 0 0 0 0 0 g 0 0 0 0 0 0 0 0 h 0 0 0 0 0 0 1 0 e1 a e2 e3 e5 e7 d f b e4 c e6 e g e8 h Graph descriptions : adjacency list A list of out-going vertices is associated to each vertex Compact representation Optionally, a list of in-going vertices can be added to allow reverse-traversal of the graph Vertex a b c d e f g h out (b,b) (c,e) () (d) (b,c) () (h) () in () (a,e) (b,e) (d) (b) () () (h) e1 a e2 e3 e5 e7 d f b e4 c e6 e g e8 h Graph descriptions : formal description one row per edge one column for heads one column for tails optional columns for edge attributes (label, weight, color, …) e1 a e2 e3 e5 e7 d f b e4 c e6 e g e8 h head tail label a b e1 a b e2 b c e3 b e e4 e b e5 e c e6 d d e7 g h e8 Basic concepts of graph theory Walks, trails and paths Jacques van Helden Jacques.van.Helden@ulb.ac.be Walk A walk from vertex A to vertex B is an alternating sequence of vertices and edges, representing a continuous traversal from A to B Remarks a r A walk can be described unequivocally by the sequence of edges (e.g.: d, e, a, d, n,p,h,t,t,t) In a non-simple graph (i.e. with multi-edges), a walk is not described unequivocally by a sequence of vertices An edge or a vertex can appear repeatedly in the same walk (e.g.: edges d and t , and vertices a, e, c on the figure) a b b g d e c f e d f i g j h m n j o i k l p l q k s t c h m Closed walk A closed walk is a walk whose initial and final vertices are identical (e.g.: d, e, a, d, n,p,h,t,t,t,b,a) a r a b b g d e c f e d f i g j h m n j o i k l p l q k s t c h m Trail a r A trail is a walk with no repeated edges (e.g.: d, e, a, c,l,q,h,t) Remark: a vertex can appear repeatedly in the same trail (e.g.: a and c on the figure) a b b g d e c f e d f i g j h m n j o i k l p l q k s t c h m Path a r A path is a trail with no repeated vertices, except possibly the initial and final vertex (e.g. c,l,q,h) a b b g d e c f e d f i g j h m n j o i k l p l q k s t c h m Path a r A cycle is a closed path with at least one edge (e.g. c,l,q,h,b,a) a b b g d e c f e d f i g j h m n j o i k l p l q k s t c h m Connected graph a connected graph is a graph in which there is a walk between every pair of distinct vertices Connected graph a r a b b g d e c f e d f i g j h m n j o i k l p l q k s Non-connected graph t a c c d h r k a b b g d e f e f i g j h m n j o i l m t c q k m s Basic concepts of graph theory Trees Jacques van Helden Jacques.van.Helden@ulb.ac.be Tree a tree is a connected graph that has no cycles Tree a b a d e c d k m li l k f n b f i g c j h j p m Rooted tree A rooted tree is a directed tree having a distinguished vertex r called the root such that for each other vertex v, there is a directed path from the root to v Each non-root node has a single parent Rooted tree root=a d c d k e m li l k d b e f f j Depth root = a c i g j h o e m b h d k e i 0 c k 1 l l 2 f f p 3 i m o j g 4 j h 5 p m 6 c 7 h Queue and stack A queue is a sequence of elements such that each new element is added (enqueued) to one end, called the back of the queue, and an element is removed (dequeued) from the other end, called the front A stack is a sequence of elements such that each new element is added (or pushed) onto one end, called the top, and an element is removed (popped) from the same end Level-order tree traversal with a queue Enqueue root While queue is not empty Dequeue a vertex and write it to the output list Enqueue its children left-to-right a e d i b k f g j m c h l Step 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Output a e d i b k l f g j h m c Queue a e,d d,i,b i,b,k,l b,k,l k,l,f l,f f g j,h h,m m,c c 1 2 3 4 5 6 8 9 10 12 13 11 7 Pre-order tree traversal with a stack Push root onto the stack While stack is not empty Pop a vertex off stack, and write it to the output list Push its children right-to-left onto stack a e d i b k f g j m c h l Step 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Output a e i b f g j m c h d k l Stack a d,e d,b,i d,b d,f d,g d,h,j d,h,m d,h,c d,h d l,k l 1 2 11 3 4 12 13 5 6 7 8 9 10 Basic concepts of graph theory Spanning trees Jacques van Helden Jacques.van.Helden@ulb.ac.be Tree in a graph A tree T in a graph G is a connected subgraph which contains no cycle The edges and vertices of T are called tree-edges and treevertices A frontier-edge is a non-tree edge with one endpoint in T and one endpoint not in T. a b a c d d e m k k l i l f e f g i n c b h g j o j h p q m Spanning tree A spanning tree is a tree which contains all the vertices of a graph A spanning tree does not necessarily contain all the edges A fundamental cycle in the graph G is the unique cycle which is created when a non-tree edge is added to the spanning tree T Each non-tree edge corresponds to a fundamental cycle in the graph a b a c d d e m k k l i l f e f g i n c b h g j o j h p q m Depth-first-search (DFS) Initialize tree at a given vertex (for example a) Initialize the set of frontier edges as empty Set dfnumber(a) to 0 Initialize label counter i to 1 b a a (0) c d d e m k k l i l f e f g i n c b h g j o j h p q m i=1 Depth-first-search While T does not span G update the set of frontier edges b a a (0) c d d e m k k l i l f e f g i n c b h g j o j h p q m i=1 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b a a (0) c d d (1) e m k k l i l f e f g i n c b h g j o j h p q m i=2 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b a a (0) c d d (1) e m k k (2) l i l f e f g i n c b h g j o j h p q m i=3 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b a a (0) c d d (1) e f k (2) l i g i n m k f e h g j o j l (3) c b h p q m i=4 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b a a (0) c d d (1) e f k (2) l i g i n m k f e h g j o j l (3) c b h p q m (4) i=5 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b a a (0) c d d (1) e f k (2) l i g i n m k f e h g j o j l (3) c (5) b h p q m (4) i=6 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d d (1) e f k (2) l i g i n m k f e h g j o j l (3) c (5) b h p q m (4) i=7 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) k (2) l i g i n m k f f h g j o j l (3) c (5) b h p q m (4) i=8 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) k (2) l i (8) l (3) g i n m k f f c (5) b h g j o j h p q m (4) i=9 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) n m k k (2) l f f i (8) l (3) c (5) b g i h g j o j (9) h p q m (4) i=10 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) k (2) l f n m k f i (8) l (3) c (5) b g (10) i h g j o j (9) h p q m (4) i=11 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) k (2) l f n m k f i (8) l (3) c (5) b g (10) i h (11) g j o j (9) h p q m (4) i=12 Depth-first-search While T does not span G update the set of frontier edges select a frontier edge whose labelled endpoint has the largest possible dfnumber add this edge to the tree select the unlabelled endpoint of this edge, and set its dfnumber to i i := i+1 b (6) a a (0) c d e e (7) d (1) n m k k (2) l f f (12) i (8) l (3) c (5) b g (10) h (11) g j i o j (9) h p q m (4) i=13 Breadth-first-search (BFS) Initialize tree at a given vertex (for example a) Initialize the set of frontier edges as empty Write label 0 on vertex a Initialize label counter i to 1 b a a (0) c d d e m k k l i l f e f g i n c b h g j o j h p q m i=1 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) b a a (0) c d d (1) e m k k l i l f e f g i n c b h g j o j h p q m i=2 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b a d e (2) f m k k l f e i l g i n c b h g j o j h p q m i=3 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f m k k l f e i l g i n c b h g j o j h p q m i=4 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f m k k (4) l f e i l g i n c b h g j o j h p q m i=5 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f m k k (4) l f e i l (5) g i n c b h g j o j h p q m i=6 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f m k k (4) l f e i (6) l (5) g i n c b h g j o j h p q m i=7 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f m k k (4) l f e i (6) l (5) n c b g i o j (7) h g j h p q m i=8 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f (8) m k k (4) l f e n i (6) l (5) c b g h g j i o j (7) h p q m i=9 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f (8) m k k (4) l f e n i (6) l (5) c (9) b g h g j i o j (7) h p q m i=10 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f (8) m k k (4) l f e n i (6) l (5) c (9) b g h g j i o j (7) h p q m (10) i=11 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f (8) m k k (4) l f e n i (6) l (5) c (9) b g (11) h g j i o j (7) h p q m (10) i=12 Breadth-first-search While T does not span G update the set of frontier edges select a frontier edge for which the labeled endpoint has the smallest possible label add edge to the tree T select the unlabelled vertex of the edge and set its label to i increment i (i := 1+1) a (0) c d (1) b (3) a d e (2) f (8) m k k (4) l f e n i (6) l (5) c (9) b g (11) h (12) g j i o j (7) h p q m (10) i=13 Result comparison : DFS versus BFS b (6) c (5) b a a (0) c de (7) e d (1) m k n f f (12) i (8) l g (10) g j i o j (9) h q m (4) b (3) c d (1) d e e (2) k k (4) l n i (6) l (5) g (11) f f (8) m c (9) b a a (0) DFS p l (3) k (2) h (11) h (12) g j i o j (7) h p q m (10) BFS Dijkstra's Shortest path finding algorithm Input: a weighted connected graph G whose edge-weights are nonnegative, and a starting vertex a Output: a spanning tree, rooted at a, whose path from each vertex v is the shortest path from a to v in G; the vertex-labelling gives the distance from s to each vertex b 7 a 2 3 d e k 7 2 f 4 3 6 2 3 i l c 8 g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm Initialise the Dijkstra tree T as vertex a dist[a] = 0 write label 0 on vertex a b 7 a (0) 2 3 d e k 7 2 f 4 3 6 2 3 i l c 8 g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) a (0) 3 (3) e 2 (2) d k 7 2 3 i l c 8 2 f 4 3 6 b 7 (7) g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 3 (3) e 2 (2) d (2) k 7 2 f i l c 8 2 3 4 3 6 b 7 (7) g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) b 7 (7) a (0) 3 (3) e 2 (2) d (2) k (8) 7 2 f 4 3 6 2 3 i l (9) c 8 g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 2 (2) d (2) 3 (3) e k i 7 (9) l c 8 2 3 2 f 4 3 6 (8) b 7 (7) g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 7 (7) 2 (2) 3 (3) e (3) d (2) k i 7 (9) l c 8 2 3 2 f 4 3 6 (8) b g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 7 (7) 2 (2) 3 (3) e (3) d (2) k i 7 (9) l c 8 2 3 (6) f 2 4 (7) 3 (6) 6 (8) b g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 7 (7) 2 (2) 3 (3) e (3) d (2) 3 (6) 6 (8) k i (6) 7 (9) l b c 8 2 3 (6) f 2 4 (7) g 2 4 j h 4 9 9 11 m Dijkstra's Shortest path finding algorithm While T does not span G Update frontier edges For each frontier edge e • • let x be the labelled and y the unlabelled endpoints of e set P(e) = dist[x] + w(e) Let e be a frontier edge that has the smallest P-value Add edge e to T, and set dist[y] = P(e) a (0) 7 (7) 2 (2) 3 (3) e (3) d (2) 3 (6) 6 (8) k i (6) 7 (9) l b (6) c 8 2 3 (6) f 2 4 (7) g 2 4 j h 4 9 9 11 m Graph theory Topological properties of graphs Jacques van Helden Jacques.van.Helden@ulb.ac.be Graph types Homogeneous networks Each pair of nodes is connected with a constant random probability The connectivity follows a Poisson law • • • P(k) ~ lx*e-k/x! l mean number of connections per node k number of connections for a given node The probability of finding a highly connected node decreases exponentially with connectivity Scale-free networks The network contains a few highly connected nodes, and most other nodes are weakly connected Can be generated randomly with a model where new nodes are preferentially connected to already established nodes The connectivity follows a power law • • • P(k) ~ k-g g k number of connections for a given node Suggested readings Gross, J. & Yellen, J. (1999). Graph theory and its applications. Discrete mathematics and its applications (Rosen, K. H., Ed.), CRC press, London Basic concepts of graph theory Exercises Jacques van Helden Jacques.van.Helden@ulb.ac.be Adjacency matrix a. b. c. d. Draw a graph on the basis of the adjacency matrix below. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs on the graph. Apply Dijkstra algorithm to find the shortest path from A to D in the graph. (note: for the exam, another adjacency matrix will be used) from\to A B C D E F G H A 0 4 2 0 0 0 0 0 B 0 0 0 9 10 0 0 0 C 4 0 0 0 8 0 0 0 D 0 8 0 0 0 0 1 0 E 3 0 0 0 0 0 8 0 F 0 0 4 0 0 3 0 0 G 0 0 0 0 0 0 0 3 H 0 0 0 0 9 2 0 2 Answer: adjacency matrix D B 8 A 4 3 9 10 1 4 E 8 2 C 8 4 9 3 3 G H 2 2 F Tree traversal Vertex A B C D E F G H I J K L M out (E,D) () () (I,H) (B,G) () (C,F) () (K,M) () (L,J) () () a. b. c. Draw a graph corresponding to the adjacency list below. Which type of graph does it represent ? Label the nodes according to the pre-order traversal algorithm below. Indicate at each step the state of the stack and the output. Push root onto the stack While stack is not empty • Pop a vertex off stack, and write it to the output list • Push its children right-to-left onto stack Tree traversal - answer root = A E D B G I H C F K M L J Exam March 2002 - question 1 a. b. Draw a graph on the basis of the adjacency matrix below. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs on the graph. Apply Dijkstra algorithm to find the shortest path from A to H in the graph. c. from\to A B C D E F G H A B C D E F G H 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 8 2 0 0 0 0 0 0 0 5 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 9 3 1 5 0 A 1 3 B 8 5 5 1 3 2 C 2 E 0 D 9 F 1 3 G 5 H 1 3 1 8 5 5,4 2 5 1 9,6 3 2 2 9 9,7 1 3 5 5 11,10,9,8 Exam March 2002 - question 2 a. b. c. Draw the tree corresponding to the adjacency list below. Which node is the root ? Label the nodes according to the pre-order traversal algorithm given below. Indicate at each step the state of the stack and the output. Vertex A B C D E F G H I J K Push root onto the stack While stack is not empty • Pop a vertex off stack, and write it to the output list • Push its children right-to-left onto stack out (K,G) () (D,A) () (C,F) () (H) () () () (J,B,I) E C F D A K J G B I H Step 0 1 2 3 4 5 6 7 8 9 10 11 stack out E F,C E F,A,D C F,A D F,G,K A F,G,I,B,J K F,G,I,B J F,G,I B F,G I F,H G F H F 1 2 11 3 4 5 6 9 7 8 10 Exam June 2002 - question 1 a. b. Draw a graph on the basis of the adjacency matrix below. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs on the graph. Apply Dijkstra algorithm to find the shortest path from A to H in the graph. c. from\to A B C D E F G H A B C D E F G H 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 0 7 0 2 0 0 0 0 0 0 8 0 0 0 1 0 0 0 0 5 0 0 0 1 3 0 0 0 1 0 0 0 0 1 0 0 0 0 4 0 0 0 0 0 0 3 5 5 0 A 1 5 3 B 8 C 4 1 E 3 7 2 D 5 F 5 3 H 1 1 5 G Exam June 2002 - question 2 a. b. c. Draw the tree corresponding to the adjacency list below. Which node is the root ? Label the nodes according to the pre-order traversal algorithm given below. Indicate at each step the state of the stack and the output. Vertex A B C D E F G H I J K Push root onto the stack While stack is not empty • Pop a vertex off stack, and write it to the output list • Push its children right-to-left onto stack out (G) () (D,A,K) () (C,F,A) () (H) (B,I) () () (J) E C D F A K G J H B I Exam June 2002 - question 3 a. b. c. Draw the tree corresponding to the adjacency list below. Which node is the root ? Label the nodes according to the level-order traversal algorithm given below. Indicate at each step the state of the queue and the output. Enqueue root While queue is not empty Vertex A B C D E F G H I J K Dequeue a vertex and write it to the output list Enqueue its children left-to-right out (G,E) () (D,A,K) () (C,F) () (H) (B,I) () () (J) A G E C H F B D K J I Exam June 2002 - question 4 a. b. c. Draw an example of path Draw an example of trail which is not a path Draw an example of walk which is not a trail Exam September 2002 - question 1 a. b. Draw a graph on the basis of the adjacency matrix below. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs on the graph. Apply Dijkstra algorithm to find the shortest path from A to H in the graph. c. A from\to A B C D E F G H A B C D E F G 1 H 1 5 5 1 B 2 4 1 0 1 C 5 1 1 5 4 1 1 1 10 2 4 1 4 2 4 1 4 10 7 1 2 2 D 1 1 10 H 7 E 10 1 2 G 2 F 3 1 1 10 12 7 10 1 2 9 4 2 11 Exam September 2002 - question 2 a. b. c. Draw the tree corresponding to the adjacency list below. Which node is the root ? Label the nodes according to the level-order traversal algorithm given below. Indicate at each step the state of the queue and the output. Enqueue root While queue is not empty Vertex A B C D E F G H I J K out (B,C) (D,E,F) (G) () () () (H,I) () (J,K) () () Dequeue a vertex and write it to the output list Enqueue its children left-to-right A C B D Step out queue 0 A 1 A B,C 2 B C,D,E,F 3 C D,E,F,G 4 D E,F,G 5 E F,G 6 F G 7 G H,I 8 H I 9 I J,K 10 J K E F G I H 11 J K 1 3 2 4 5 6 7 9 8 K 10 11 Exam July 2003 - question 1 a. b. Draw a graph on the basis of the adjacency matrix below. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs on the graph. Apply Dijkstra algorithm to find the shortest path from A to B in the graph. c. from\to A B C D E F G H A B C D E F 9 G A H 1 5 4 1 (1) 1 G (1) 3 5 (5) 3 (4) 9 (9) H (4) 4 (8) D (8) 1 15 (16) 11 (15) 1 15 10 4 9 11 A 3 1 G 15 F 5 11 10 C 1 1 F (13) 9 H 3 4 B 9 (13) 3 (11) 10 (14) 4 D 9 3 1 E 1 (13) 1 (14) C (12) 1 (12) 4 (16) B (14) E (11) Exam July 2003 - question 2 a. b. c. Draw the tree corresponding to the adjacency list below. Which node is the root ? Label the nodes according to the pre-order traversal algorithm given below. Indicate at each step the state of the stack and the output. Push root onto the stack While stack is not empty • Pop a vertex off stack, and write it to the output list • Push its children right-to-left onto stack Vertex A B C D E F G H I J K out (I,N,H) () () (J) (P,G) () () () (D) () () M (K) N A N I M D H E Step 0 1 2 3 4 5 6 7 8 9 10 stack out A H,N,I A H,N,D I H,N,J D H,N J H,E,M N H,E,K M H,E K H,G,P E H,G,C,Q P H,G,C,F,B Q 11 H,G,C,F B 12 H,G,C F 13 H,G C (M,E) 14 H G P (Q,C) 15 Q (B,F) J K P Q B G C F H 1 5 2 6 3 4 7 15 8 9 14 10 13 11 12