GRAPHS http://collectionsonline.nmsi.ac.uk/detail.php? t=objects&type=related&kv=8083246
GRAPHS
http://collectionsonline.nmsi.ac.uk/detail.php?t=objects&type=related&kv=8083246
BASIC TERMINOLOGY
• A Graph is composed of:• Nodes (aka Vertices)• Edges (aka Arcs)• Directed or Undirected (aka bi-directional)• May have an associated cost
• Representation• Adjacency Matrix• Adjacency List
Alice
Calvin
Dee
Bob
53
1
4
SOME COMMON APPLICATIONS / ALGORITHMS
• Visiting every node in some order:• Depth-first and Breadth-first search
• Finding the shortest path from each node to another• Network packet routing• Floyd-Warshall algorithm
• Visiting each node in the most efficient path possible and return to start (travelling salesman)• UPS delivery• [No polynomial-time solution!] Several greedy / heuristic
algorithms give good solutions.
• **Find the shortest path from A to B.• Google maps• Djikstra's algorithm (and A*)
SEARCH TREES
• If we are doing some kind of search of a graph, we often need to construct a search tree.• Note: often multiple search trees for one graph.
Search Tree
0
3
4
1
5 6
0
3
4
1
2
5 6
Graph
6.0
3.01.0
9.04.0
4.03.0
Start == 0, Goal == 6
2.0
class SearchNode{ GraphNode mTwin; SearchNode mParent; float mTotalCost; [float mHeuristic;] [int mPQueuePos;]}
BREADTH-FIRST SEARCH
• Maintain a Queue of SearchNode's: frontier• Initially with just the starting node's s.node.
• Maintain a HashSet of visited SearchNodes: visited• At each update:
1. Create an empty Queue of SearchNodes: new_frontier.2. Iterate through the frontier for each S.Node C:
a. If C is the goal, construct the solution path.b. For each neighbor, N, of C:
• If N isn't on visited, add it to new_frontier (and make the parent = C).
3. Replace frontier with new_frontier.
• Problem: we will get to the goal, but it won't (necessarily) be the optimal path.
BREADTH-FIRST SEARCHS
0
8
6
3
547
1
2
G
10.0
3.5
4.5
2.0
2.03.0
4.5
6.0
5.0
4.0
5.0
5.0
7.5
1.5
6.0
3.5
3.0
DEPTH-FIRST SEARCH
• Often done recursively.• However, it is often more efficient to use a Stack
(of SearchNode's)• Also maintain a HashSet (like in B.F.S.): visited• At each update:
1. Peek at the top search node from the stack: C.2. If C is the goal, construct the solution path.3. Else:
a. Look at each Neighbor, N, of C:i. If C isn't in visited, add it to visited and push onto the stack.
b. Pop C off the stack.
DEPTH-FIRST SEARCH
S
0
8
6
3
547
1
2
G
10.0
3.5
4.5
2.0
2.03.0
4.5
6.0
5.0
4.0
5.0
5.0
7.5
1.5
6.0
3.5
3.0
Total=17
Total=19
A* ALGORITHM
• A* is often called a "best-first" search.• At each stage, we pick the most promising node:• Ordered by the sum of cost-so-far and heuristic• Heuristic: the estimate of how far to the goal.
• Often the straight-line distance.
• We store the nodes on a PriorityQueue (OpenList) for fast-access.
1. Create a single search node for the start and put on p.queue
2. Repeat as long as p.queue is not empty:a. Pull the best node, C, off the p.queue.b. For each Neighbor, N, of C:
i. if N hasn't been explored, create a search node and add to p.queue.ii. If N is on open, see if the path through C is better. If so:
a. Update the parent and cost-so-farb. Reheapify that node on the OPEN list.