1 / 16

abstract containers

sequence/linear (1 to 1). first ith last. hierarchical (1 to many). graph (many to many). set. abstract containers. G = (V, E). a vertex may have: 0 or more predecessors 0 or more successors.

tincher
Download Presentation

abstract containers

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. sequence/linear (1 to 1) first ith last hierarchical (1 to many) graph (many to many) set abstract containers

  2. G = (V, E) a vertex may have: 0 or more predecessors 0 or more successors

  3. some problems that can be represented by a graph • computer networks • airline flights • road map • course prerequisite structure • tasks for completing a job • flow of control through a program • many more

  4. graph variations • undirected graph (graph) • edges do not have a direction • (V1, V2) and (V2, V1) are the same edge • directed graph (digraph) • edges have a direction • <V1, V2> and <V2, V1> are different edges • for either type, edges may be weighted or unweighted

  5. A E B C D a digraph V = [A, B, C, D, E] E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]

  6. graph data structures • storing the vertices • each vertex has a unique identifier and, maybe, other information • for efficiency, associate each vertex with a number that can be used as an index • storing the edges • adjacency matrix – represent all possible edges • adjacency lists – represent only the existing edges

  7. storing the vertices • when a vertex is added to the graph, assign it a number • vertices are numbered between 0 and n-1 • graph operations start by looking up the number associated with a vertex • many data structures to use • any of the associative data structures • for small graphs a vector can be used • search will be O(n)

  8. 0 ABCDE 0 1 2 3 4 A 4 1 E B C D 3 2 the vertex vector

  9. 0 ABCDE 0 1 2 3 4 A 4 1 E B 0 1 2 3 4 0 1 2 3 4 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 C D 3 2 adjacency matrix

  10. a V2 matrix is needed for a graph with V vertices maximum # edges?

  11. many graphs are “sparse” • degree of “sparseness” key factor in choosing a data structure for edges • adjacency matrix requires space for all possible edges • adjacency list requires space for existing edges only • affects amount of memory space needed • affects efficiency of graph operations

  12. 0 ABCDE 0 1 2 3 4 A 4 1 E B C D 3 2 2 3 adjacency lists 0 1 2 3 4 1 2 4 1

  13. Some graph operations adjacency matrixadjacency lists O(e) O(e) O(e) O(E) insertEdge isEdge #successors? #predecessors? O(1) O(1) O(V) O(V) Memory space used?

  14. Where to start? Will all vertices be visited? How to prevent multiple visits? traversing a graph ny bos dc la chi atl

  15. ny bos dc la chi atl breadth first traversal breadthFirstTraversal (v) put v in Q while Q not empty remove w from Q visit w mark w as visited for each neighbor (u) of w if not yet visited put u in Q

  16. depth first traversal ny bos dc la chi atl depthFirstTraversal (v) visit v mark v as visited for each neighbor (u) of v if not yet visited depthFirstTraversal (u)

More Related