cisc 4080 computer algorithms spring 2017 note spring
play

CISC 4080 Computer Algorithms Spring, 2017 Note Spring/2017 1 What - PDF document

CISC 4080 Computer Algorithms Spring, 2017 Note Spring/2017 1 What is graph? A graph is a set of vertices (or nodes) V , and a set of edges E between pairs of vertices. Graph represents some kind of binary relation among a set. If the


  1. CISC 4080 — Computer Algorithms Spring, 2017 Note Spring/2017 1 What is graph? A graph is a set of vertices (or nodes) V , and a set of edges E between pairs of vertices. Graph represents some kind of binary relation among a set. If the relation is symmetric, then the graph is undirected, meaing that edges have no direction. If the relation is not symmetric, then the graph is directed, and edges are represented as ordered pairs and drawn with an arraw on the line. So a directed graph G can be represented by G = ( V, E ), where V = { v 1 , v 2 , ..., v n } , E = { ( v i , v j ) | v i is connected to v j } . An undirected graph G can be represented by G = ( V, E ), where V = { v 1 , v 2 , ..., v n } , E = {{ v i , v j }| v i is connected to v j and vice versa. } Exercise 1. Which can be represented as undirected graph, directed graph? • Vertices: email addresses, edges: one address sendings email to another address • vertices: cities and towns, edges: roads connecting cities and towns • vertices: countries, edges: sharing a border • vertices: courses, edges: one class is prerequisite of the other • vertices: classes, edges: two classes are offered at same time block • vertices: classes, edges: two claees are taught by same instructor • vertices: Web sites, edges: a web site has a hyperlink to another site • Game tree/graph where each vertex represents a game/puzzle configuration, and each edge rep- resents that a single movecan take the game from one configuration to another • and so on and on ... Exercise 2 : Can you draw the game tree/graph of tic-tac-toe game? Note that this graph can be generated by a program.

  2. 2 Graph Theory: a subdiscipline of computer science and math that studies property and algorithms for graph problems. • Graph Coloring Problem: color vertices of a given graph so that two vertices connected by an edge are of different color. • Graph Exploring/Search/discovery: find all nodes that can be reached (via edges in the graph) from a given node. • Shortest Path problem: find paths from a source node to a destination node with the smallest hop. • network flow problem: • Minimum spanning tree: • and so on and on. 3 Computer Representation of Graph and memory usage Here we focus on representing the connectivities of the graph (information about each node and edge can be stored in arrays and vectors as needed), i.e., how to represent information about which node is connected with which other node. • Adjacency Matrix is a 2D array, with | V | (i.e., the number of vertices) rows and columns, where the matrix element A i,j is 1 if vertex i is connected to vertex j , is 0 otherwise. template <class NodeType> class Graph { .... private: bool directed; //true: directed graph, false: undirected graph vector<NodeType> nodes; //edges[i] stores all nodes that nodes[i] is connected to int * adjacentMatrix[]; //a NxN array, where N is the number of nodes, i.e., // the length of nodes vector }; A undirected graph with four vertices a, b, c, d, where a is connected to c and d, and b is connected to d. V={"a","b","c","d"} Adjacency matrix: 4x4 matrix | 0 1 2 3 -------------------- 0 | 1 | 2 | 3 | 2

  3. – Observation: adjacent matrix of an undirected graph is symmetric along the major diagonal line . – Space requirement: – Time complexity of finding out whether a node is connected to another node? • Adjacency Lists stores the list of vertices that a node i is connected to in a linked list, and organze the adjacency lists of all vertices in the graph as a vector or array. template <class NodeType> class Graph { .... private: bool directed; //true: directed graph, false: undirected graph vector<NodeType> nodes; //edges[i] stores all nodes that nodes[i] is connected to vector<list<int>> edges; }; For the example above, here is the adjacency lists representation (Note that we store each edge twice, for example, if node a and b are connected by an edge, b is in the adjacent list of a, and a is in the adjacent list of b. – Space requirement: – Time complexity of finding out whether a node is connected to another node? • Sometimes, a graph is not fully generated beforehand, but instead is gradually expanded during graph exploration process following the underlying logic, as illustrated by the following example. For example, when walking a maze, the underlying graph is explored as you try to find a way from starting point to exit point. Exercise 3 : How would you represent the following maze as a graph? *S**_** *___*** **__*** **____* **_**E* * represents a dead end, _ represents a space, S, E are the starting and exit points of the maze. 3

  4. Hint: use a vertex to represent each "space", and connect space points with an edge if you the two points are connected. Exercise 4 : From “Problems for the Quickening of the Mind” (compiled about A.D. 775). There are a wolf, a goat, a bag of cabbage, and a ferryman. From an initial position on the left bank of a river, the ferryman is to transport the wolf, the goat, and the cabbage to the right bank. The difficulty is that the ferrymans boat is only big enough for him to transport one object at a time, other than himself. Yet, for obvious reasons, the wolf cannot be left alone with the goat, and the goat cannot be left alone with the cabbage. How should the ferryman proceed? How do you model this problem as a graph problem? 4

  5. 3 Search (Explore) undirected graph. The problem: Given a graph, and a vertex in the graph, find out what parts of the graph are reachable from the vertex. Recall that the graph is given as adjacency matrix or adjacency lists, which allows you to find out whether a node is connected to another node, or find out the list of nodes that a node is connected to. Exercise 5 : We are interested in systematic ways to explore the graph so that every vertex is vis- ited exactly one. Two common algorithms for traversing or searching tree or graph data structures. • Breadth-first search (BFS): starts at the given vertex, and explores the neighbor nodes first, before moving to the next level neighbors (i.e., we visit nodes that are one-hop away from the starting node, and then visit nodes that are two-hop away, and then nodes that are three-hop away. We discover nodes in this order until there are all nodes that are reachable have been visited). For the above graph, if we perform BFS from node A, the following tree illustrates how we discover each node. The path from A to each node in the tree is the path in the original graph via which we reach the node in BFS. A --> D ---> G | | | --> H --> C --> F | --> B --> E ---> I | --> J – Note that when a node have multiple neighbors, we arbitrarily decide which to visit first. – Note that the paths that’s discovered by BFS are the shortest path from the source node to each node in the graph. • Depth-first search (DFS): starts at the given vertex, and explores as far as possible along each branch before backtracking . In the above graph, under DFS traverse from node A, the paths that lead to each node are illustrated in the following DFS tree. A ---> D ---> G ---> H | --> C ---> F ---> B ---> E ---> J ---> I 5

  6. – backgracking means that when node u has no unvisited neighbor, we go back to the node v that leads us to node u in the first place. For example, in the above example, after visiting node H , which have two neighbors ( G and D ) that have already been visited, we backtrack to G . As all neighbors of G are also visited already, we backtrack to D , and so on. After we backtrack to A , we find A has unvisited neighbors B, C . We pick one to visit and explore first (in above exmaple, we pick C first). 4 Implementing BFS and DFS 1. Breadth First Search (application: web crawling, finding optimal solution to maze and other puzzles, ...) • By-product of the algorithm: shortest distance (hop-count) path from source to each reachable node, breadth-first-search tree. • Correctness of the algorithm: • Running time of the algorithm: • Implementation: 1) we can use a queue for frontier . 2) Due to the first-in-first-out nature of queue, we can use the same queue for frontier and next frontier , and get rid of i . How to modify the line marked as (HERE) in the code, i.e., how to set v ’s level? BFS_Explore (G, s) // Explore and visit all vertices of graph G // that are reachable from node s in BFS order // (Courtesy Eric Demaine, MIT) // Idea: Use frontier to store the nodes in current level // level[v]: store the number of hops of v (how many hops does // it take to go from s to v) // parent[v]: store v’s parent (where we come from) // The above two arrays could be implemented using hash table level[s]=0 //you can use C++ STL map to store level and parent parent[s]=none; frontier.push (s); //put s into the queue while frontier!=NULL { u = frontier.front(); frontier.pop(); //remove the front element for each v in Adj(u) //for each v that is in u’s adjacency list if level of v is not set, i.e., v has not been visited { level[v] = level[u]+1 parent[v] = u frontier.push (v) } } 2. Depth-first Search (application: ) 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend