Professional Documents
Culture Documents
Structures and
Strategies for
3 State Space
Search
•
•
John’s meals are spicy.
If it doesn’t rain on Monday, Tom will go to
mountains.
• All basketball player are tall
• Some people like chocolates
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 1 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 2
Amelia Ritahani Ismail Amelia Ritahani Ismail
Predicate Calculus
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 3 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 4
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 5 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 6
Amelia Ritahani Ismail Amelia Ritahani Ismail
1
Introduction Introduction- Graph Theory
• The theory of state space search is our primary tool • What is a graph?
for answering these questions. – It consists of a set of nodes and a set of arcs or
• By representing a problem as a state space graph, we links connecting pairs of nodes
can use graph theory to analyze the structures and
complexity of both the problem and the search
• In the state space of problem solving:
procedures that we employ to solve it. – The nodes are taken to represent discrete
(isolated) states
– The arcs are representing transition between
states
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 7 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 8
Amelia Ritahani Ismail Amelia Ritahani Ismail
Introduction Introduction
• Example: Fig 3.1: The city of Königsberg
– The Swiss mathematician – Leonhard Euler invented graph
theory to solve the “bridges of Königsberg problem”
(Newman 1956)
– The city of Königsberg occupied both banks and 2 islands of
a river.
– The islands and the riverbanks were connected by 7 bridges
as indicated in the next slide. The city is divided by a river.
There are two islands at the river. The first island is
connected by two bridges to both riverbanks and is also
connected by a bridge to the other island. The second island
is connected by two bridges each connecting to one
riverbank.
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 9 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 10
Amelia Ritahani Ismail Amelia Ritahani Ismail
Introduction Introduction
Fig 3.2: Graph of the Königsberg bridge system
• The problem:
– “If there is a walk around the city that crosses
NODES:
each bridge exactly one”
• Euler created an alternative representation for
the map, as indicated in Fig 3.2 (next slide).
ARCH:
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 11 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 12
Amelia Ritahani Ismail Amelia Ritahani Ismail
2
Definition of a graph Definition of a graph
• Each bridge requires 2 connect predicates, one for
• A graph consists of each direction in which the bridge may be crossed.
• A predicate expression (below) indicating that any
– A set of nodes (can be infinite) bridge can be crossed in either direction.
– A set of arcs that connect pairs of nodes. connect(X, Y, Z) = connect(Y, X, Z)
• An arc is an ordered pair, e.g., e.g.: connect(i1, i2, b1) = connect (i2, i1, b1)
(i1, rb1), Therefore, we can remove the second one.
(rb1, i1).
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 13 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 14
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 15 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 16
Amelia Ritahani Ismail Amelia Ritahani Ismail
A and C are
ancestors of
state g, h and I
Arch of a
graph is
Graph Theory And
G, h and I are
labeled / descendants
weight to
indicate A tree us a
relationship Rooted graph has a root:
graph in
(usually drawn at the top,
above other nodes and which two
there is a path from the nodes have at
root to all nodes within the most one
graph.
Graph is path between
directed if it them. Trees
arcs have often have
associated Relationship roots
between nodes
directionality include parent,
(arrow) child and siblings
( parent
preceding its Siblings
child)
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 17 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 18
Amelia Ritahani Ismail Amelia Ritahani Ismail
3
Definition of a graph Definition of a graph
• A graph consists of nodes and arcs. • A leaf or tip node is a node that has no children
• If a directed arc connects N and M, N is called the (sometimes also called a dead end).
parent, and M is called the child. If N is also connected • A path of length n is an ordered sequence of n+1
nodes such that the graph contains arcs from each
to K, M and K are siblings. node to the following ones.
• A rooted tree has a unique node which has no parents. E.g., [a b e] is a path of length 2.
The edges in a rooted tree are directed away from the • On a path in a rooted graph, a node is said to be an
root. Each node in a rooted tree has a unique parent. ancestor of all the nodes positioned after it (to its
right), as well as a descendant of all nodes before it
(to its left).
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 19 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 20
Amelia Ritahani Ismail Amelia Ritahani Ismail
• A path that contains any node more than Directed: Ordered pair of vertices. Represented as (u, v) directed from
vertex u to v.
once is said to contain a cycle or loop.
• A tree is a graph in which there is a u v
unique path between every pair of nodes. Undirected: Unordered pair of vertices. Represented as {u, v}.
Disregards any sense of direction and treats both end vertices
• Two nodes are said to be connected if a interchangeably.
path exists that includes them both.
u v
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 21 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 22
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 23 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 24
Amelia Ritahani Ismail Amelia Ritahani Ismail
4
Definitions – Graph Type Definitions – Graph Type
Multigraph: G(V,E), consists of set of vertices V, set of Edges Pseudograph: G(V,E), consists of set of vertices V, set of
E and a function f from E to {{u, v}| u, v V, u ≠ v}. The Edges E and a function F from E to {{u, v}| u, v Î V}.
edges e1 and e2 are called multiple or parallel edges if f Loops allowed in such a graph.
(e1) = f (e2). Representation Example: V = {u, v, w}, E = {e1, e2, e3, e4}
Representation Example: V = {u, v, w}, E = {e1, e2, e3}
u
e1 w e4
u e2
e1 e2
w
v e3
e3
v
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 25 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 26
Amelia Ritahani Ismail Amelia Ritahani Ismail
u v
u
u e4
e1 e2
w e3
u
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 27 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 28
Amelia Ritahani Ismail Amelia Ritahani Ismail
Type Edges Multiple Edges Loops Allowed ? • Example: Undirected Graph G (V, E)
Allowed ?
Simple Graph v u w
u
Multigraph v 0 1 1
Pseudograph
u 1 0 1
v w
Directed Graph
w 1 1 0
Directed
Multigraph
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 29 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 30
Amelia Ritahani Ismail Amelia Ritahani Ismail
5
Representation- Adjacency Matrix
• Example: directed Graph G (V, E) • Draw a simple graph with adjacency matrix
with respect to the ordering vertices a, b, c
u
v u w and d.
v 0 1 0
0 1 1 1
u 0 0 1
v w 1 0 1 0
w 1 0 0
1 1 0 0
1 0 0 0
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 31 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 32
Amelia Ritahani Ismail Amelia Ritahani Ismail
G = (V, E) be an unditected graph. Suppose that v1, v2, v3, …, vn are the
• From the directed graph below, find an •
vertices and e1, e2, …, em are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the nx m matrix M = [m ij], where
adjacency matrix for the given directed graph
below: 1 when edge ej is incident with vi
m ij =
0 otherwise
Can also be used to represent :
Multiple edges: by using columns with identical entries, since these edges
are incident with the same pair of vertices
Loops: by using a column with exactly one entry equal to 1, corresponding
to the vertex that is incident with the loop
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 33 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 34
Amelia Ritahani Ismail Amelia Ritahani Ismail
Graph descriptions :
Representation- Incidence Matrix
Find the adjacency matrix
• one row per vertex
• Representation Example: G = (V, E) • one colum per vertex
• value = 1 if vertices are adjacent
e1 e2 e3
from\to a b c d e f g h
u a 0 1 0 0 0 0 0 0
v 1 0 1
e1 e2 b 0 0 1 0 1 0 0 0 e1
u 1 1 0
c 0 0 0 0 0 0 0 0 e3
w 0 1 1 d 0 0 0 1 0 0 0 0 a e2
b c
v w
e 0 1 1 0 0 0 0 0 e5
e3 e7 e4 e6
f 0 0 0 0 0 0 0 0
g 0 0 0 0 0 0 0 1 d e
h 0 0 0 0 0 0 0 0
f g e8 h
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 35 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 36
Amelia Ritahani Ismail Amelia Ritahani Ismail
6
Graph descriptions : incidence matrix
Graph descriptions : adjacency list
• one row per edge • A list of out-going vertices is associated to each
• one colum per vertex vertex
• value = 1 if edge and vertex are incident • Compact representation
• Optionally, a list of in-going vertices can be
edge\vertex a b c d e f g h added to allow reverse-traversal of the graph
e1 1 1 0 0 0 0 0 0 e1
e2 1 1 0 0 0 0 0 0 e1 Vertex out in e3
e3 0 1 1 0 0 0 0 0 a (b,b) () a e2
b c
e3 b (c,e) (a,e)
e4 0 1 0 0 1 0 0 0 a e2
b c e7
e5
e5 0 1 0 0 1 0 0 0 c () (b,e) e4 e6
e5
e6 0 0 1 0 1 0 0 0 e7 e4 e6 d (d) (d) d e
e (b,c) (b)
e7 0 0 0 0 1 0 0 0 d e f () ()
e8 0 0 0 0 0 0 1 1 f g e8 h
g (h) ()
f g e8 h h () (h)
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 37 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 38
Amelia Ritahani Ismail Amelia Ritahani Ismail
STATE SPACE SEARCH • The start state represents the initial problem
• Each state represents some configuration reachable
from the start state
• Some states may be goal states (solutions)
– A set of operators
• Applying an operator to a state transforms it to
another state in the state space
• Not all operators are applicable to all states
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 39 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 40
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 41 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 42
Amelia Ritahani Ismail Amelia Ritahani Ismail
7
State space of the 8-puzzle (repeated) The 8-puzzle problem as state space
search
• states: possible board positions
• operators: one for sliding each square in each of four
directions,
or, better, one for moving the blank square in each of
four directions
• initial state: some given board position
• goal state: some given board position
• Note: the “solution” is not interesting here, we need
the path.
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 43 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 44
Amelia Ritahani Ismail Amelia Ritahani Ismail
Strategies for State Space Search Strategies for State Space Search
Strategies for State Space Search Strategies for State Space Search
Goal-driven search
Data-driven search is suggested if: • Sometimes called backward chaining.
• All or most of the data are given in the initial problem • Goal-driven reasoning focuses on the goal, finds the rules
that could produce the goal, and chains backward through
statement. successive rules and sub-goals to the given facts of the
• There are a large number of potential goals but there are problem.
only a few ways to use the facts and given information of
a particular problem instance.
• It is difficult to form a goal or hypothesis.
8
Strategies for State Space Search Strategies for State Space Search
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 49 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 50
Amelia Ritahani Ismail Amelia Ritahani Ismail
Depth-first Search
• In this search, when a state is examined, all of its
children and their descendants are examined before any
of its siblings.
• This search goes deeper into the search space whenever
this is possible.
• Only when no further descendants of a state can be
found are its siblings are considered.
Fig 3.12: Graph for breadth - and depth - first search examples
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 51 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 52
Amelia Ritahani Ismail Amelia Ritahani Ismail
Strategies for State Space Search Strategies for State Space Search
The search:
Depth-first Search A, B, E, K, S, L, T, Breadth-first Search
F, M, C, G, N, H, O, P, • In contrast to depth-first search, it explore the space in
U, D, I, Q, J, R a level-by-level fashion.
• Only when there are no more states to be explored at a
given level does the algorithm move on to the next
The
level.
backtrack
algorithm
implemented
in depth-first
search
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 53 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 54
Amelia Ritahani Ismail Amelia Ritahani Ismail
9
DEPTH FIRST SEARCH
Strategies for State Space Search :pre-order tree traversal with a stack
• Push root onto the stack
The search: • While stack is not empty
Breadth-first Search A, B, C, D, E, F, G, – Pop a vertex off stack, and write it to the output list
H, I, J, K, L, M, N, – Push its children right-to-left onto stack
1
O, P, Q, R, S, T, U α Step Output Stack
0 α 2 11
ε δ 1 α δ,ε
2 ε δ,β,ι
ι β κ λ 3 ι δ,β 3 4 12 13
4 β δ,φ
φ 5 φ δ,γ 5
6 γ δ,η,ϕ
γ 7 ϕ δ,η,µ 6
ϕ η 8 µ δ,η,χ
9 χ δ,η 7 10
µ 10 η δ
11 δ λ,κ 8
χ 12 κ λ
9
13 λ
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 55 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 56
Amelia Ritahani Ismail Amelia Ritahani Ismail
Breadth-first Search a. Draw the tree corresponding to the adjacency list below.
α
Step Output Queue
ε δ 4 5 6 7 Vertex out
0 α
1 α ε,δ A (K,G)
ι β κ λ 2 ε δ,ι,β 8 B ()
3 δ ι,β,κ,λ C (D,A)
φ 4 ι β,κ,λ D ()
5 β κ,λ,φ 9
γ E (C,F)
6 κ λ,φ
F ()
ϕ η 7 λ φ 10 11 G (H)
8 φ γ
µ H ()
9 γ ϕ,η
10 ϕ η,µ 12 I ()
χ J ()
11 η µ,χ
12 µ χ 13 K (J,B,I)
13 χ
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 57 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 58
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 59 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 60
Amelia Ritahani Ismail Amelia Ritahani Ismail
10
An instance of the traveling Search of the traveling salesperson problem. (arc
salesperson problem label = cost from root)
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 61 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 62
Amelia Ritahani Ismail Amelia Ritahani Ismail
• Shortest-Path problems • Input: a weighted connected graph G whose edge-weights are non-negative, and
a starting vertex α
– Single-source (single-destination). Find a shortest path • Output: a spanning tree, rooted at α, whose path from each vertex v is the
from a given source (vertex s) to each of the vertices. The shortest path from α to v in G; the vertex-labelling gives the distance from s to
topic of this lecture. each vertex
β 8 χ
– Single-pair. Given two vertices, find a shortest path 7
α
between them. Solution to single-source problem solves this
problem efficiently, too. 2 3 3 2 4
ε γ η
δ φ 2 2
– All-pairs. Find shortest-paths for every pair of vertices.
Dynamic programming algorithm. 3 4 4 9
– Unweighted shortest-paths – BFS.
6 7 ι ϕ 9
λ 11
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 65 7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 66
Amelia Ritahani Ismail Amelia Ritahani Ismail
11
Dijkstra's Shortest path finding Dijkstra's Shortest path finding algorithm
algorithm • While T does not span G
– Update frontier edges
• Initialise the Dijkstra tree T as vertex α
– For each frontier edge e
• dist[αα] = 0 • let x be the labelled and y the unlabelled endpoints of e
• write label 0 on vertex α • set P(e) = dist[x] + w(e)
β 8 χ β χ
7 7 (7) 8
α (0) α (0)
2 3 3 2 4 2 (2) 3 (3) 3 2 4
ε γ η ε γ η
δ φ 2 2 δ φ 2 2
3 4 4 9 3 4 4 9
6 7 ι ϕ 9 6 7 ι ϕ 9
λ 11 λ 11
7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 67 7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 68
Amelia Ritahani Ismail Amelia Ritahani Ismail
Dijkstra's Shortest path finding algorithm Dijkstra's Shortest path finding algorithm
• While T does not span G • While T does not span G
– Update frontier edges – Update frontier edges
– For each frontier edge e – For each frontier edge e
• let x be the labelled and y the unlabelled endpoints of e • let x be the labelled and y the unlabelled endpoints of e
• set P(e) = dist[x] + w(e) • set P(e) = dist[x] + w(e)
– Let e be a frontier edge that has the smallest P-value – Let e be a frontier edge that has the smallest P-value
– Add edge e to T, and set dist[y] = P(e) – Add edge e to T, and set dist[y] = P(e)
β χ β χ
7 (7) 8 7 (7) 8
α (0) α (0)
3 4 4 9 3 4 4 9
6 7 ι ϕ 9 6 7 ι ϕ 9
λ 11 λ (9) 11
7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 69 7/27/2006 κ (8) CSC 3301- Semester 1 (2006-2007) - µ 70
Amelia Ritahani Ismail Amelia Ritahani Ismail
Dijkstra's Shortest path finding algorithm Dijkstra's Shortest path finding algorithm
• While T does not span G • While T does not span G
– Update frontier edges – Update frontier edges
– For each frontier edge e – For each frontier edge e
• let x be the labelled and y the unlabelled endpoints of e • let x be the labelled and y the unlabelled endpoints of e
• set P(e) = dist[x] + w(e) • set P(e) = dist[x] + w(e)
– Let e be a frontier edge that has the smallest P-value – Let e be a frontier edge that has the smallest P-value
– Add edge e to T, and set dist[y] = P(e) – Add edge e to T, and set dist[y] = P(e)
β χ β χ
7 (7) 8 7 (7) 8
α (0) α (0)
3 (3) 3 2 3 (3) 3 2
2 (2) 4 2 (2) 4
ε γ η ε (3) γ η
δ (2) φ 2 2 δ (2) φ 2 2
3 4 4 9 3 4 4 9
6 (8) ι ϕ 9 6 (8) ι ϕ 9
7 (9) λ 7 (9) λ
11 11
7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 71 7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 72
Amelia Ritahani Ismail Amelia Ritahani Ismail
12
Dijkstra's Shortest path finding algorithm Dijkstra's Shortest path finding algorithm
• While T does not span G • While T does not span G
– Update frontier edges – Update frontier edges
– For each frontier edge e – For each frontier edge e
• let x be the labelled and y the unlabelled endpoints of e • let x be the labelled and y the unlabelled endpoints of e
• set P(e) = dist[x] + w(e) • set P(e) = dist[x] + w(e)
– Let e be a frontier edge that has the smallest P-value – Let e be a frontier edge that has the smallest P-value
– Add edge e to T, and set dist[y] = P(e) – Add edge e to T, and set dist[y] = P(e)
β χ β χ
7 (7) 8 7 (7) 8
α (0) α (0)
3 (3) 2 3 (3) 2
2 (2) 3 (6) 4 2 (2) 3 (6) 4
ε (3) γ η ε (3) γ η
δ (2) φ 2 2 δ (2) φ 2 2
Dijkstra's Shortest path finding algorithm b. Knowing that each cell of the matrix indicates the weight of an arc, label the arcs
on the graph.
c. Apply Dijkstra algorithm to find the shortest path from A to H in the graph.
• 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 A 0
– Add edge e to T, and set dist[y] = P(e) 1 5 1 5
from\to A B C D E F G H
β (6) χ A 1 5 B
1
C 1
1
5
7 (7) 8
α (0) B 2 4
3 (3) 2 C 1 4 2 4 1 4 2 4 1 4
2 (2) 3 (6) 4
ε (3) γ η D 1 10
δ (2) φ 2 2 D
1 7
G 3
1 7
9
E 1 1 10 7 1
E 1
4
3 (6) 4 (7) 4 F 1
9 10 10 2 10 10 2
G 2 1 1
H 2 H F 12 11
6 (8) ι (6) ϕ 9 2 2
7 (9) λ
11
7/27/2006 κ CSC 3301- Semester 1 (2006-2007) - µ 75 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 76
Amelia Ritahani Ismail Amelia Ritahani Ismail
Using the State Space to Represent State Space Description of a Logical System
• First example of how a set of logical relationships may be
Reasoning with the Predicate viewed as defining graph is from the propositional calculus.
Calculus • If p, q, r, …. are propositions, assume the assertions
(statement) :
• q -> p
• r -> p
• v -> q
• s -> r
• t -> r
• s -> u
• s
• t
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 77 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 78
Amelia Ritahani Ismail Amelia Ritahani Ismail
13
Using the State Space to Represent Reasoning Using the State Space to Represent Reasoning
with the Predicate Calculus with the Predicate Calculus
State Space Description of a Logical System State Space Description of a Logical System: And/Or
q -> p graphs
r -> p • In the expression of the form (below) both q and
v -> q r must be true for the p to be true.
s -> r
– q ^ r -> p
t -> r
s -> u • In the expression of the form (below), the truth
s of either q or r is sufficient to prove p is true
t – q v r -> p
Fig 3.13: State space graph of a set of implications in the
propositional calculus.
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 79 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 80
Amelia Ritahani Ismail Amelia Ritahani Ismail
Fig 3.14: And/or graph of the Fig 3.15: And/or graph of the
expression q Λ r → p. expression q v r → p .
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 81 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 82
Amelia Ritahani Ismail Amelia Ritahani Ismail
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 83 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 84
Amelia Ritahani Ismail Amelia Ritahani Ismail
14
a. Draw a graph on the basis of the adjacency matrix below.
b. Knowing that each cell of the matrix indicates the weight of an arc, label the
arcs on the graph.
c. Apply Dijkstra algorithm to find the shortest path from A to B in the graph.
Exercise from\to
A
A B C D
9
E F G
1 5
H
B
C 4 1
D 3
E 1
F 1
G 15 3
H 10 4 9 11
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 85 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 86
Amelia Ritahani Ismail Amelia Ritahani Ismail
a. Draw the tree corresponding to the adjacency list below. a. Draw the tree corresponding to the adjacency list below.
b. Which node is the root ? b. Which node is the root ?
c. Label the nodes according to the pre-order traversal c. Label the nodes according to the level-order traversal algorithm
algorithm given below. Indicate at each step the state of the given below. Indicate at each step the state of the queue and the
stack and the output. output.
Push root onto the stack Enqueue root
While stack is not empty While queue is not empty
• Pop a vertex off stack, and write it to the output list Dequeue a vertex and write it to the output list
Enqueue its children left-to-right
• Push its children right-to-left onto stack
Vertex out
Vertex out
A (B,C)
A (K,G)
B (D,E,F)
B () C (G)
C (D,A) D ()
D () E ()
E (C,F) F ()
F () G (H,I)
G (H) H ()
H () I (J,K)
I () J ()
J () K ()
K (J,B,I)
7/27/2006 CSC 3301- Semester 1 (2006-2007) - 87 7/27/2006 CSC 3301- Semester 1 (2006-2007) - 88
Amelia Ritahani Ismail Amelia Ritahani Ismail
END OF CHAPTER 3
15