1 / 30

COSC 2007 Data Structures II

COSC 2007 Data Structures II. Chapter 14 Graphs II. Topics. Graph implementation Adjacency matrix adjacency list Traversal Depth-first search Breadth-first search. ADT Graphs.

ohunt
Télécharger la présentation

COSC 2007 Data Structures II

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. COSC 2007Data Structures II Chapter 14 Graphs II

  2. Topics • Graph implementation • Adjacency matrix • adjacency list • Traversal • Depth-first search • Breadth-first search

  3. ADT Graphs • Insertion & deletion operations are somewhat different for graphs than for other ADTs in that they apply to either nodes or edges • Elements: • A graph consists of nodes and edges. Each node will contain one data element • Each node is uniquely identified by the key value of the element it contains • Structure: • An edge is a one-to-one relationship between a pair of distinct nodes • A pair of nodes can be connected by at most one edge, but any node can be connected to any collection of other nodes

  4. ADT Graphs • Operations: • Create an empty graph • Determine whether a graph is empty • Determine the number of nodes in a graph • Determine the number of edges in a graph • Determine whether an edge exists between two nodes • Insert a node/ an edge • Delete a node/ an edge • Retrieve a node having a given search key • Traverse a graph

  5. Implementation of Graphs • Two common implementations: • Adjacency matrix • Adjacency list • Adjacency Matrix (Incidence Matrix) • A graph containing n nodes can be represented by an n x n matrix of Boolean values or 1/0 • M[i,j] = 1 (T) <==> If there is an edge between node i & node j of the graph. 0 (F) Otherwise

  6. A B L C P R Implementation of Graphs • Adjacency Matrix • Example:

  7. Implementation of Graphs • Adjacency Matrix • The ith row and ith column are identical (Symmetric matrix) • Fewer than half the entries are needed, since each edge is recorded twice and the diagonal contains all zeros • Using a lower triangular matrix is possible, but it is not convenient • If the graph is directed, the full matrix, except the diagonal, is needed

  8. Implementation of Graphs • Adjacency Lists • Keep an array of linked lists, one points to one LL • For each node, in the linked list, there is an edge list of the neighbors of that node • Any other possibility?

  9. Implementation of Graphs • Maintain each adjacency list as a linked chain • Array h head nodes keeps track of the lists • h[i] points to the first node of adjacency list for vertex i G 1 2 3 4

  10. Implementation of Graphs • Questions:

  11. 2 3 1 4 5 Graph Traversal • Process each node in a graph exactly once (if & only if the graph is connected) • The order of visiting the nodes is not unique, because it depends on the representation

  12. B C A F D E Graph Traversal • An adjacency list representation of the graph could lead to many different orderings of the visit, depending on the sequence in which the edges are entered • If a graph contains a cycle, a graph-traversal algorithm can loop indefinitely, unless the visited nodes are marked

  13. 2 3 1 4 5 Graph Search Methods • A vertex u is reachable from vertex v iff there is a path from v to u. 8 9 10 6 7 11

  14. 2 3 8 1 4 5 9 10 6 7 11 Graph Search Methods • A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v.

  15. Graph Search Methods • Many graph problems solved using a search method • Path from one vertex to another • Is the graph connected? • Etc. • Commonly used search methods: • Depth-first search • Breadth-first search

  16. Graph Traversal • Depth First Search (DFS) • Follow a path from a previous node as far as possible before moving to the next neighbor of the starting node • DFS uses a stack to put all nodes to be traversed (ready stack). • Idea of DFS algorithm • “Mark" a node as soon as we visit it • Try to move to an unmarked adjacent node • If there are no unmarked nodes at the present position, backtrack along the nodes already visited, until a node is reached that is adjacent to an unvisited node • Visit this node • Continue the process

  17. Graph Traversal • Depth First Search (DFS) • Recursive dfs(in v:Vertex) // Traverses a graph beginning at node v by using a // depth-first search (Recursive version) Mark v as visited for (each unvisited node u adjacent to v) dfs(u)

  18. 2 2 3 8 1 1 4 5 9 10 6 7 11 Depth-First Search Example Start search at vertex 1.

  19. Graph Traversal • Depth First Search (DFS) • Iterative (Using a Stack) dfs(v) // Traverses a graph beginning at node v by using a DFS s.CreateStack( ) s.push ( v) // Push v onto stack & mark it Mark v as visited while (!s.isEmpty( ) ) { if (no unvisited nodes are adjacent to v on stack top) s.pop () // Backtrack else { Select an unvisited node u adjacent to the node on top of stack s.push (u) Mark u as visited } // end if } // end while

  20. Graph Traversal • Breadth First Search (BFS) • Look at all of the neighbors of a node first, then follow the paths from each of these nodes to its neighbors, and so on • Nodes that have been visited but not explored are kept in a (ready) queue, so that when we are ready to move to a node adjacent to the current node, we will be able to return to another node adjacent to the old current node after our move

  21. Graph Traversal • Breadth First Search (BFS) • Iterative: Using Queue bfs( v:Vertex) // Traverses a graph beginning at node v by using a // Breadth-first search q.createQueue ( ) q.enqueue(v) // Add v to queue & mark it Mark v as visited while (!q.isEmpty ( ) ) { q.dequeue(w) for (each unvisited node u adjacent to w) { Mark u as visited q.enqueue(u) } // end for } // end while

  22. Breadth-First Search • Visit start vertex and put into a FIFO queue. • Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.

  23. Review • A graph-traversal algorithm stops when ______. • it first encounters the designated destination vertex • it has visited all the vertices that it can reach • it has visited all the vertices • it has visited all the vertices and has returned to the vertex that it started from

  24. Review • A ______ is the subset of vertices visited during a traversal that begins at a given vertex. • circuit • multigraph • digraph • connected component

  25. Review • An iterative DFS traversal algorithm uses a(n) ______. • list • array • Queue • stack

  26. Review • An iterative BFS traversal algorithm uses a(n) ______. • list • array • Queue • stack

  27. Review • A ______ is an undirected connected graph without cycles. • tree • multigraph • digraph • connected component

  28. Review • A connected undirected graph that has n vertices must have at least ______ edges. • n • n – 1 • n / 2 • n * 2

  29. Review • A connected undirected graph that has n vertices and exactly n – 1 edges ______. • cannot contain a cycle • must contain at least one cycle • can contain at most two cycles • must contain at least two cycles

  30. Review • A tree with n nodes must contain ______ edges. • n • n – 1 • n – 2 • n / 2

More Related