DOC PREVIEW
MSU CSE 830 - Lecture12

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

GraphsSlide 2Graph InterpretationsMore interpretationsFriendship GraphsGraph TerminologyTerminology continued...Still More Terminology...Yet More Terminology...Adjacency MatricesAdjacency ListsTradeoffs Between Adjacency Lists and Adjacency MatricesGraph SquaringG2 with Adjacency MatricesG2 with Adjacency ListsTraversing a GraphMarking VerticesCorrectness of Graph TraversalTraversal OrdersExample GraphBreadth-First Search TreeKey BFS Tree PropertyQuestions about BFS treeDepth-First Search TreeDirected Graph DFS treesQuestionSlide 27Topological SortingToplogical SortingGraphs•Motivation and Terminology•Representations•Traversals•Variety of ProblemsGraphsA graph G consists of a set of vertices V together with a set E of vertex pairs or edges.G = (V,E) [in some texts they use G(V,E)].Typically n nodes and m edgesGraphs are important because any binary relation is a graph, so graphs can be used to represent essentially any relationship.Graph InterpretationsLiving Room Den Bedroom Kitchen Hallway BathLiving DenRoom Hall Bed Kitchen BathApartment BlueprintThe vertices could represent rooms in a house, and the edges could indicate which of those rooms are connected to each other.Sometimes a using a graph will be an easy simplification for a problem.More interpretations•Vertices are cities and edges are the roads connecting them.•Edges are the components in a circuit and vertices are junctions where they connect.•Vertices are software packages and edges indicate those that can interact.•Edges are phone conversations and vertices are the households being connected.Friendship GraphsAuntie M.Tin ManThe Wicked Witchof the WestEach vertex represents a person, and each edge indicates that the two people are friends.DorothyScarecrowCowardly LionGraph TerminologyDirected and undirected graphsA graph is said to be undirected if edge (x, y) always implies (y, x). Otherwise it is said to be directed. Often called an arc.Loops, multiedges, and simple graphsAn edge of the form (x, x) is said to be a loop. If x was y’s friend several times over, we can model this relationship using multiedges. A graph is said to be simple if it contains no loops or multiedges.Weighted edgesA graph is said to be weighted if each edge has an associated numerical attribute. In an unweighted graph, all edges are assumed to be of equal weight.Terminology continued...PathsA path is a any sequence of edges that connect two vertices. A simple path never goes through any vertex more than once. The shortest path is the minimum number edges needed to connect two vertices.ConnectivityThe “six degrees of separation” theory argues that there is always a short path between any two people in the world. A graph is connected if there is there is a path between any two vertices. A directed graph is strongly connected if there is always a directed path between vertices. Any subgraph that is connected can be referred to as a connected component.Still More Terminology...Degree and graph typesThe degree of a vertex is the number of edges connected to it. The most popular person will have a vertex of the highest degree. Remote hermits may have degree-zero vertices. In dense graphs, most vertices have high degree. In sparse graphs, most vertices have low degree. In a regular graph, all vertices have exactly the same degree.CliqueA graph is called complete if every pair of vertices is connected by an edge. A clique is a sub-graph that is complete.Yet More Terminology...Cycles and DagsA cycle is a path where the last vertex is adjacent to the first. A cycle in which no vertex is repeated is said to be a simple cycle. The shortest cycle in a graph determines the graph’s girth. A simple cycle that passes through every vertex is said to be a Hamiltonian cycle. An undirected graph with no cycles is a tree if it is connected, or a forest if it is not. A directed graph with no directed cycles is said to be a directed acyclic graph (or a DAG)Adjacency Matrices143521 2 3 4 51 0 1 1 1 02 1 0 0 1 03 1 0 0 1 14 1 1 1 0 15 0 0 1 1 0Adjacency Lists14352123452 3 41 41 2 3 51 4 53 4Tradeoffs Between Adjacency Lists and Adjacency MatricesComparison Winner (for worst case)Faster to test if (x, y) exists? matrices: (1) vs. (n) Faster to find vertex degree? lists: (1) vs. (n)Less memory on sparse graphs? lists: (m+n) vs. (n2)Less memory on dense graphs? matrices: (small win)Edge insertion or deletion? matrices: (1) vs. (n)Faster to traverse the graph? lists: (m+n) vs. (n2)Better for most problems? listsGraph SquaringThe square of a directed graph G = (V, E) is the graph G2 = (V, E2), such that (x, y)  E2 iff, for some z, both (x, z) and (z, y)  E ; i.e., there is a path of exactly two edges.Give efficient algorithms to square a graph on both adjacency lists and matrices.G2 with Adjacency MatricesGiven an adjacency matrix, we can check in constant time whether a given edge exists. To discover whether there is an edge (x, y) in E2, for each possible intermediate vertex z we can check whether (x, z) and(z, y) exist in O(1).Since there are O(n) intermediate vertices to check, and O(n2) pairs of vertices to ask about, this takes O(n3) time.G2 with Adjacency ListsFor a given edge (x, z), we can run through all the edges from z in O(n) time, and fill the results into an adjacency matrix, which is initially empty.It takes O(mn) to construct the edges, and O(n2) to initialize and read the adjacency matrix, for a total of O((n+m)n). Since m+1  n (unless the graph is disconnected), this is usually simplified to O(mn), and is faster than the previous algorithm on sparse graphs.Traversing a GraphOne of the most fundamental graph problems is to traverse every edge and vertex in a graph. Applications include: - Printing out the contents of each edge and vertex. - Counting the number of edges. - Identifying connected components of a graph. For correctness, we must do the traversal in a systematic way so that we don't miss anything. For efficiency, we must make sure we visit each edge at most twice.Marking VerticesThe idea in graph traversal is that we mark each vertex when we first visit it, and keep track of what is not yet completely explored. For each vertex, we maintain two flags: - discovered - have we encountered this vertex before? - explored - have we


View Full Document

MSU CSE 830 - Lecture12

Download Lecture12
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture12 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture12 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?