Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
courses:cs211:winter2014:journals:colin:chapter3 [2014/01/30 03:32] – created mohnacsc | courses:cs211:winter2014:journals:colin:chapter3 [2014/02/02 23:55] (current) – mohnacsc | ||
---|---|---|---|
Line 2: | Line 2: | ||
==== 3.1 - Basic Definitions and Applications ==== | ==== 3.1 - Basic Definitions and Applications ==== | ||
+ | |||
+ | A graph is made up of nodes and edges. | ||
+ | |||
+ | Applications of graphs include transportation networks, communication networks, information networks, social networks, and dependency networks. | ||
+ | |||
+ | A path traversing a graph is simple if all its vertices are distinct from one another. | ||
+ | |||
+ | An undirected graph is connected if there is a path for every pair of nodes. | ||
+ | |||
+ | A tree is an undirected graph not containing a cycle. | ||
+ | |||
+ | Every n-node tree has exactly n-1 edges. | ||
+ | |||
+ | Let G be an undirected graph on n nodes. | ||
+ | - G is connected | ||
+ | - G does not contain a cycle | ||
+ | - G has n-1 edges | ||
+ | |||
==== 3.2 - Graph Connectivity and Graph Traversal ==== | ==== 3.2 - Graph Connectivity and Graph Traversal ==== | ||
+ | |||
+ | === Breadth-First Search === | ||
+ | |||
+ | Explored outward from root adding one ‘layer’ at a time. The root and all nodes joined to the root make up the first later. | ||
+ | |||
+ | For each j >= 1, layer L(j) produced by BFS consists of all nodes at distance j from root (s). There is a path from s to t iff t appears in some layer. | ||
+ | |||
+ | Let T be a BFS tree, let x and y be nodes in T belonging to layers L(i) and L(j) respectively, | ||
+ | |||
+ | The set produced at the end of the algorithm is precisely the connected component containing the starting node. The actual path is recovered by finding the targeted node then tracing back to the starting node. | ||
+ | |||
+ | === Depth-First Search === | ||
+ | |||
+ | In depth-first search, begin at the root and follow the leading edge until a dead end. Backtrack until you find a node with an unexplored neighbor. | ||
+ | |||
+ | For a given recursive call DFS(u), all nodes that are marked “Explored” between the invocation and end of this recursive call are descendants of u in T. Let x and y be nodes in T, and let (x,y) be an edge of G that is not an edge of T. Then one of x or y is an ancestor of the other. | ||
+ | |||
+ | === The Set of All Connected Components === | ||
+ | |||
+ | For any two nodes s and t in a graph, their connected components are either identical or disjoint. | ||
+ | |||
==== 3.3 - Implementing Graph Traversal Using Queues and Stacks ==== | ==== 3.3 - Implementing Graph Traversal Using Queues and Stacks ==== | ||
+ | A graph is represented by adjacency matrix or adjacency list. To build a graph, sets of nodes and edges are needed. | ||
+ | |||
+ | An adjacency list is better for sparse graphs (graphs with less than n^2 edges). | ||
+ | |||
+ | Queue is first-in, first-out order. | ||
+ | |||
+ | The BFS algorithm runs in time O(m+n) if the graph is given by the adjacency list representation. | ||
+ | |||
+ | The DFS algorithm visits each node using a reverse adjacency list. It runs in time O(m+n) if the graph is given by the adjacency list representation. | ||
+ | |||
+ | |||
+ | === Discussion === | ||
+ | |||
+ | The breadth-first and depth-first search algorithms are pretty straight forward and have predictable run times. | ||
+ | |||
+ | |||
+ | ==== 3.4 - Testing Bipartiteness: | ||
+ | |||
+ | A bipartite graph is one where the node set V can be partitioned into sets X and Y in such a way that every edge has one end in X and the other end in Y. If a graph is bipartite, then it cannot contain an odd cycle. | ||
+ | |||
+ | We can test a graph for bipartiteness by choosing some node s and coloring it red. The neighbors of s must be colored blue and, in turn, we must make the neighbors of blue nodes red. We continue this process until we have visited every node, similar to the BFS algorithm. | ||
+ | |||
+ | Let G be a connected graph. | ||
+ | * There is no edge of G joining two nodes of the same layer. | ||
+ | * There is an edge of G joining two nodes of the same layer. | ||
+ | |||
+ | |||
+ | ==== 3.5 - Connectivity in Directed Graphs ==== | ||
+ | |||
+ | A directed graph in adjacency list representation has two associated lists for each node: nodes to which it has edges and nodes from which it has edges. | ||
+ | |||
+ | |||
+ | ==== 3.6 - Directed Acyclic Graphs and Topological Ordering ==== | ||
+ | |||
+ | If a directed graph has no cycles it is a directed acyclic graph (DAG). | ||
+ | |||
+ | In every DAG G, there is a node v with no incoming edges. | ||
+ | |||
+ | |||
+ | === Discussion === | ||
+ | With the expansion of the BFS and DFS applications, |