130 likes | 235 Views
Searching the Web. The web can be considered a graph “ the web graph ” Web pages are the graph nodes Hyperlinks on pages are graph edges The web graph is huge (way over 8 billion nodes) - even infinite (pages are created on the fly)
E N D
Searching the Web • The web can be considered a graph “the web graph” • Web pages are the graph nodes • Hyperlinks on pages are graph edges • The web graph is huge (way over 8 billion nodes) - even infinite (pages are created on the fly) • For the web graph, DFS is not a good strategy (you get lost quickly) • You need to search your neighborhood before going deeper
Syllabus Assignments Documentation CS Dept. Brian Text PLTC ... ... ... ... ... ... The Web Graph - Starting at CS230 Home CS230
S A H I J C B D E G F Breadth-First Search (BFS): Neighbors First • In DFS you keep the nodes you visited on a stack so that you can find your way back • In BFS you keep the nodes* you visited on a queue so that you can explore them in the order you found them • At every visited node you also keep track of the direct path that took you there from S in a list* actually the paths to the nodes, not just the nodes Initialization While you have not reached G remove path from BFS queue and check at the last node L in the path extend the path to unvisited neighbors of L and add extended paths to back of queue
S[S] A [SA] H [SH] I [SHI] J [SHJ] C [SAC] B [SAB] D [SACD] E [SACE] G[SHJG] F BFS: Queue of Paths S SA SH SH SAB SAC SAB SAC SHI SHJ SAC SHI SHJ SHI SHJ SACD SACE SHJ SACD SACE Initialization While you have not reached G remove path from BFS queue and check at the last node L in the path extend the path to unvisited neighbors of L and add extended paths to back of queue SACD SACE SHJG SACE SHJG … in two more steps it will reach the goal
S[S] A [SA] H [SH] I [SHI] J [SHJ] C [SAC] B [SAB] D [SACD] E [SACE] G[SHJG] F searchGraph(S, G) Initialization currentPath = the path under consideration currentPath.add([S]); visited = list of visited nodes visited.add(S); pathQueue = queue of paths under consideration pathQueue.enQ([S]); while (the goal G has not yet been found) { deQ a path and set it to currentPath lastNode = last node of path if (the goal is reached) { a solution has been found } else { get the neighbors of the lastNode remove any neighbors that already have been visited create extended path to each neighbor add these paths to pathQueue if (there are no paths left) { there is no solution } } }
BFS: Initialization publicstatic LinkedList searchGraph (LinkedList graph, String start, String goal) { // finds a path from start node to goal node in the graph // using the breadth-first search strategy LinkedList currentPath = new LinkedList(); LinkedList visited = new LinkedList(); // list of visited nodes visited.add(start); LinkedList neighbors = new LinkedList(); // list of neighbor nodes Queue pathQueue = new Queue(); // paths under consideration currentPath.add(start); // create initial path and add to queue pathQueue.enQ(currentPath); String lastNode, neighbor; boolean found = false; // set to true if path to goal is found ... }
BFS: The While Loop publicstatic LinkedList searchGraph (LinkedList graph, String start, String goal) { ... while (!found) { // while the goal has not yet been found... currentPath = (LinkedList)pathQueue.deQ(); // remove path at front of queue lastNode = (String)currentPath.getLast(); // get last node of path if (lastNode.equals(goal)) { // goal is reached found = true; return currentPath; } else { // get neighbors of the last node neighbors = getNeighborList(lastNode, graph); // remove neighbors that have already been visited removeVisitedNeighbors(neighbors, visited); // create extended path to each neighbor and add it to queue if (!neighbors.isEmpty()) for (int i = 0; i < neighbors.size(); i++) { neighbor = geti(neighbors, i); visited.add(neighbor); pathQueue.enQ(extendPath(currentPath, neighbor)); } if (pathQueue.empty()) { // if there are no paths left, no solution System.out.println("Path cannot be found"); return currentPath; } } } }
A Capital Idea in Just- spring when the world is mud- luscious the little lame balloonman whistles far and wee and eddieandbill come running from marbles and piracies and it's spring when the world is puddle-wonderful the queer old balloonman whistles far and wee and bettyandisbel come dancing from hop-scotch and jump-rope and it's spring and the goat-footed balloonMan whistles far and wee IN JUST- SPRING WHEN THE WORLD IS MUD- LUSCIOUS THE LITTLE LAME BALLOONMAN WHISTLES FAR AND WEE AND EDDIEANDBILL COME RUNNING FROM MARBLES AND PIRACIES AND IT'S SPRING WHEN THE WORLD IS PUDDLE-WONDERFUL THE QUEER OLD BALLOONMAN WHISTLES FAR AND WEE AND BETTYANDISBEL COME DANCING FROM HOP-SCOTCH AND JUMP-ROPE AND IT'S SPRING AND THE GOAT-FOOTED BALLOONMAN WHISTLES FAR AND WEE
Reading From a File import java.io.*; import java.util.*; publicclass FileTest { // code to demonstrate use of file reading publicstatic void readTextFile (String fileName) { Queue que = new Queue(); try { // set up file for reading line by line FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String S = br.readLine(); // read first line of text while (S != null) { que.enQ(S); // add new line of text to que S = br.readLine(); // read next line of text } fr.close(); // close file } catch (IOException e) { System.out.println("error in reading text file"); } while (que.size() > 0) //print text in queue System.out.println(((String)(que.deQ())).toUpperCase()); } publicstatic void main (String[] args) { readTextFile("eec.txt"); } }
Simulation • Simulation is a technique for modeling the behavior of both natural and human-made systems • Goal • Generate statistics that summarize the performance of an existing system • Predict the performance of a proposed system • Example • A simulation of the behavior of a bank
Application: Simulation • An event-driven simulation • Simulated time is advanced to the time of the next event • Events are generated by a mathematical model that is based on statistics and probability • A time-driven simulation • Simulated time is advanced by a single time unit • The time of an event, such as an arrival or departure, is determined randomly and compared with a simulated clock
Application: Simulation • The bank simulation is concerned with • Arrival events • Indicate the arrival at the bank of a new customer • External events: the input file specifies the times at which the arrival events occur • Departure events • Indicate the departure from the bank of a customer who has completed a transaction • Internal events: the simulation determines the times at which the departure events occur
Application: Simulation • An event list is needed to implement an event-driven simulation • An event list • Keeps track of arrival and departure events that will occur but have not occurred yet • Contains at most one arrival event and one departure event Figure 7.14 A typical instance of the event list