Art_of_Programming_Contest_Part8

Art_of_Programming_C - CHAPTER 12 GRAPHS 143 Complete Graph A graph is said to be complete if there is an edge between every pair of vertices

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 12 GRAPHS 143 Complete Graph A graph is said to be complete if there is an edge between every pair of vertices. Bipartite Graph A graph is said to be bipartite if the vertices can be split into two sets V1 and V2 such there are no edges between two vertices of V1 or two vertices of V2. Uninformed Search Searching is a process of considering possible sequences of actions, first you have to formulate a goal and then use the goal to formulate a problem. A problem consists of four parts: the initial state , a set of operators , a goal test function, and a path cost function. The environment of the problem is represented by a
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CHAPTER 12 GRAPHS 144 state space . A path through the state space from the initial state to a goal state is a solution . In real life most problems are ill-defined, but with some analysis, many problems can fit into the state space model. A single general search algorithm can be used to solve any problem; specific variants of the algorithm embody different strategies. Search algorithms are judged on the basis of completeness , optimality , time complexity , and space complexity . Complexity depends on b , the branching factor in the state space, and d , the depth of the shallowest solution. This 6 search type below (there are more, but we only show 6 here) classified as uninformed search, this means that the search have no information about the number of steps or the path cost from the current state to the goal - all they can do is distinguish a goal state from a non-goal state. Uninformed search is also sometimes called blind search. Breadth First Serach (BFS) Breadth-first search expands the shallowest node in the search tree first. It is complete, optimal for unit-cost operators, and has time and space complexity of O(b^d). The space complexity makes it impractical in most cases. Using BFS strategy, the root node is expanded first, then all the nodes generated by the root node are expanded next, and their successors, and so on. In general, all the nodes at depth d in the search tree are expanded before the nodes at depth d+1 . Algorithmically: BFS(G,s) { initialize vertices; Q = {s]; while (Q not empty) { u = Dequeue(Q); for each v adjacent to u do { if (color[v] == WHITE) { color[v] = GRAY; d[v] = d[u]+1; // compute d[] p[v] = u; // build BFS tree Enqueue(Q,v); } } color[u] = BLACK; } BFS runs in O(V+E)
Background image of page 2
CHAPTER 12 GRAPHS 145 Note : BFS can compute d[v] = shortest-path distance from s to v, in terms of minimum number of edges from s to v (un-weighted graph). Its breadth-first tree can be used to represent the shortest-path. BFS Solution to Popular JAR Problem #include<stdio.h> #include<conio.h> #include<values.h> #define N 105 #define MAX MAXINT int act[N][N], Q[N*20][3], cost[N][N]; int a, p, b, m, n, fin, na, nb, front, rear; void init() { front = -1, rear = -1; for(int i=0; i<N; i++) for(int j=0; j<N; j++) cost[i][j] = MAX; cost[0][0] = 0; } void nQ(int r, int c, int p) { Q[++rear][0] = r, Q[rear][1] = c, Q[rear][2] = p;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 20

Art_of_Programming_C - CHAPTER 12 GRAPHS 143 Complete Graph A graph is said to be complete if there is an edge between every pair of vertices

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online