1 / 44

Data Structures

Data Structures. Graphs. Trees as Graphs. Every tree is a graph with some restrictions: The tree is directed There is exactly one directed path from the root to every node. A. B. C. D. E. F. G. H. Moving from Trees to Graphs.

gail-wiley
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Graphs

  2. Trees as Graphs • Every tree is a graph with some restrictions: • The tree is directed • There is exactly one directed path from the root to every node A B C D E F G H

  3. Moving from Trees to Graphs • Trees are useful when there is an inherent hierarchical relationship between data • sorting data by a key can build such a relationship • But, not all problems are so simple • Maybe there are complex relationships between nodes ... not just hierarchical! • this is where graphs become important

  4. Graphs • Graphs can be used as data structures • to represent how data can be related to one another • Graphs are useful models for reasoning about relations among objects and combinatorial problems • Combinatorics looks at counting the number of possibilities, and questions whether certain configurations exist, and how to get to them. • Many real-life problems can be solved by converting them to graphs. Proper application of graph theory ideas can drastically reduce the solution time for some important problems.

  5. Applications/Examples of graphs • Graphs are a good representation for any collection of objects and binary relation among them: • - The relationship in space of places or objects • - The ordering in time of events or activities • - Family relationships • - Taxonomy (e.g. animal - mammal - dog) • - Precedence (x must come before y) • - Conflict (x conflicts or is incompatible with y) • - Etc.

  6. JFK LAX STL LAX HNL DFW Applications/Examples of graphs CS16 • Electronic circuits • Networks (roads, flights, communications) FTL

  7. Applications/Examples of graphs • airport system: nodes = airports; edges = pairs of airports with non- stop flights. (weight/cost = airfare; distance; capacity) • Internet: nodes = routers; edges = links. • social graphs: (6 degrees of separation) nodes = people; edges = friends/acquaintance/co- authors • academic graphs: nodes = courses; edges = prereqs;

  8. Airline Routes Nodes = cities Edges = direct flights

  9. New York Sydney Computer Networks 56 Tokyo Seattle 128 Seoul 16 181 30 140 L.A. Nodes = computers Edges = transmission rates

  10. CSE Course Prerequisites at UW 461 312 373 143 311 332 142 415 410 331 351 413 417 421 Nodes = courses Directed edge = prerequisite 401

  11. Graph – What is it? • Graphs consist of vertices (nodes) and edges which connect the vertices. • A graph has a set vertices V, often labeled v1, v2, etc.. and a set of edges E, labeled e1, e2, ... Each edge is a pair (u, v) of vertices. • We write G = (V, E) for the graph with vertex set V and edge set E. • In applications, where pair (u, v) is distinct from pair (v, u), the graph is "directed". Otherwise, the graph is undirected. • We can convert an undirected graph to a directed one by duplicating edges, and orienting them both ways. • A path is a sequence of edges that can be taken to get from one vertex to another.

  12. Graph – What is it? • G(V,E) • Collection of vertices V and edges E • Not a totally new data structure, this is an extension from Linked List  Tree • Very useful to model many (real-life) problems • Internet • Road Traffic • Many others… • Basic Terminologies • Vertices, Edges • Path, Cycle • Directed/Un-directed • Weighted/Un-weighted • Connected/Un-connected • In-degree/Out-degree

  13. What is a Graph? • A graph G = (V,E) is composed of: V: set of vertices E: set ofedges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c e d

  14. What is a Graph? • An edge e = (u,v) is a pair of vertices • When (u, v) is an edge, we say "v is adjacent (neighbor) to u". • A loop is an edge with both endpoints being the same. • The out-degree of v = the number of neighbors of v • The in-degree of v = how many vertices have v as a neighbor. • Sometimes, edges can be associated with weights or costs. 10

  15. Concepts: Directedness • In a directed graph, the edges are “one-way.” So an edge (u, v) means you can go from u to v, but not vice versa. • In an undirected graph, there is no direction on the edges: you can go either way. (Also, no self-loops.) a self-loop

  16. Concepts: Adjacency • Two vertices are adjacent if there is an edge between them. • For a directed graph, u is adjacent to v if there is an edge (v, u). v u w v u w u is adjacent to v. v is adjacent to u and w. w is adjacent to v. u is adjacent to v. v is adjacent to w.

  17. Concepts: Degree • Undirected graph: The degree of a vertex is the number of edges touching it. • For a directed graph, the in-degree is the number of edges entering the vertex, and the out-degree is the number leaving it. The degree is the in-degree + the out-degree. degree 4 in-degree 2, out-degree 1

  18. Graph Terminology • In a graph with directed edges, the in-degree of a vertex v, denoted by deg-(v) , is the number of edges with v as their terminal vertex. • The out-degree of v, denoted by deg+(v), is the number of edges with v as their initial vertex. • Question:How does adding a loop to a vertex change the in-degree and out-degree of that vertex? • Answer:It increases both the in-degree and the out-degree by one.

  19. a b d c Graph Terminology • Example:What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph: deg-(a) = 1 deg+(a) = 2 deg-(b) = 4 deg+(b) = 2 deg-(d) = 2 deg+(d) = 1 deg-(c) = 0 deg+(c) = 2

  20. Concepts: path • A path is a sequence of edges that can be taken to get from one vertex to another. • Paths may pass through the same vertex more than once but simple paths may not. • A cycle is a path that starts and ends at the same vertex but does not pass through other vertices more than once. • Graphs are considered to be connected if there is a path between every pair of vertices.

  21. Terminology:Path • path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. a a b b c c e d e d a b e d c b e d c

  22. Terminology:Path • path: sequence of vertices v1,v2,. . .vk, such that each pair (vi, vi+1) is an edge. • The length of a path is the number of edges in it, or total weight if there are weights.

  23. More Terminology a b • simple path: no repeated vertices • cycle: simple path, except that the last vertex is the same as the first vertex b e c c d e

  24. Even More Terminology • subgraph: subset of vertices and edges forming a graph • connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. • connected graph: any two vertices are connected by some path connected not connected

  25. Graphs • A graph is complete if there is an edge between every pair of vertices. • Edges can have labels - or numeric values - which create a weighted graph. • Weights can label the distances between cities.

  26. Graphs • ADT Graph operations might include: • create an empty graph • determine if the graph is empty • insert a vertex • insert an edge between two vertices • delete a vertex • delete an edge between two vertices • retrieve the value of the data at a vertex • replace the data at a particular vertex • determine if an edge is between vertices

  27. Graph – Basic Representations • Adjacency Matrix • 2-D array of vertices • M[i][j] = 1 if there is an edge between vertex i and j, 0 otherwise • For weighted graph, M[i][j] is the weight of the edges, not just 0 or 1! • Adjacency List • An array of V linked lists • List i represent the vertices connected to vertex i • For weighted graph, the list store both neighboring vertices and their edge costs • There are more representations • But we do not have time to discuss all…

  28. Graph Representation • Show this graph’s  • Adjacency List: • Adjacency Matrix:

  29. Graphs • One common way of implementing a graph is to use an adjacency list. • Using this approach, imagine that the vertices are numbered 1,2, thru N. • This type of implementation consists of N linked lists. • There is a node in the ith linked list for the vertex j if and only if there is an edge from vertex i to vertex j.

  30. Graphs

  31. Graphs • Two common operations are to find an edge between two vertices and to find all vertices adjacent to a given vertex. • Using an adjacency list to determine whether there is an edge from one vertex to another, we must traverse the linked list associated with one of the vertices to determine whether the other vertex is present.

  32. Graphs • To determine all vertices adjacent to a given vertex, we only need to traverse the linked list associated with the specified vertex. • What about traversing a graph? • Graph-traversal starts at a specified vertex and does not stop until it has visited all of the vertices that it can reach. • It visits all vertices that have a path between the specified vertex and the next.

  33. Graphs • Notice how different this is than tree traversal. • with tree traversal, we always visit all of the nodes in the tree • with graph traversal we do not necessarily visit all of the vertices in the graph unless the graph is connected. • If a graph is not connected, then a graph traversal that begins at a vertex will only visit a subset of the graphs vertices.

  34. Graphs • There are two basic graph-traversal algorithms that we will discuss. • These algorithms visit the vertices in different orders, • but if they both start at the same vertex, they will visit the same set of vertices. • Depth first search • Breadth first search

  35. Graph Traversal • Problem: Search for a certain node or traverse all nodes in the graph • Depth First Search • Once a possible path is found, continue the search until the end of the path • Breadth First Search • Start several paths at a time, and advance in each one step at a time

  36. Graphs • Depth-First Search • This algorithm starts at a specified vertex and goes as deep into the graph as it can before backtracking. • This means that after visiting the starting vertex, it goes to an unvisited vertex that is adjacent to the one just visited. • The algorithm continues from this way until it has gotten as far as possible before returning to visit the next unvisited vertex adjacent to the starting place.

  37. Depth-First Search

  38. Graphs • Depth-First Search • This traversal algorithm does not completely specify the order in which you should visit the vertices adjacent to a particular vertex. • One possibility is to visit the vertices in sorted order. • This only works - of course - when our nodes in each linked list of an adjacency list are in sorted order.

  39. Graphs • Depth-First Search • Because the graph is connected, DFS will visit every vertex. In fact, the traversal could visit the vertices in this order: a b c d g e f h I • Notice a stack of vertices can be used to implement this approach

  40. Graphs • Breadth-First Search • This starts at a specified vertex and visits every vertex adjacent to that vertex before embarking from any of those vertices to the next set. • It does not embark from any of these vertices adjacent to the starting vertex until it has visited all possible vertices adjacent. • Notice that this is a first visited -- first explored strategy. Therefore, it should be obvious that a queue of vertices can be used

  41. Graphs • Breadth-First Search • In the above graph, starting at vertex a we could traverse the vertices in the following order: a b f i c e g d h

  42. BFS - A Graphical Representation a) b) c) d)

  43. More BFS

  44. Applications: Finding a Path • Find path from source vertex s to destination vertex d • Use graph search starting at sand terminating as soon as we reach d • Need to remember edges traversed • Use depth – first search ? • Use breath – first search?

More Related