1 / 20

Chapter 9 – Graphs

Chapter 9 – Graphs. A graph G=(V,E) – vertices and edges V is a set of vertices – nodes E is a set of edges defined as vertex pairs (u,v) where u and v are elements of V Vertex w is adjacent to vertex z if there exists an edge (w,z) in E

Télécharger la présentation

Chapter 9 – Graphs

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. Chapter 9 – Graphs • A graph G=(V,E) – vertices and edges • V is a set of vertices – nodes • E is a set of edges defined as vertex pairs (u,v) where u and v are elements of V • Vertex w is adjacent to vertex z if there exists an edge (w,z) in E • If edge (a,b) indicates we can only go from a to b, then we have a directed graph • If edge (a,b) indicates we can go either way, then we have an undirected graph. • Sometimes an edge has a weight

  2. Applications • We can use graphs to represent • Computer networks • Nodes are sites – edges are connections • Weight may be transit time • Highway system • Nodes are cities – edges are roads • Weights could be time or distance • Airline system • Nodes are cities – edges are plane routes • Weights could be cost, distance, or time

  3. More Definitions • A path is a sequence of vertices w1, w2, …, wn such that (wi,wi+1) are edges in E • The length of a path is the number of edges (n-1) • A cycle in a graph is a path of length at least 1 such that w1=wn • A simple path has all vertices distinct • A graph is acyclic if it has no cycles • DAG – directed acyclic graph

  4. Representing Graphs • We draw a graph with circles and connecting lines • Can use an adjacency matrix representation • If there are n vertices, then we create an n x n matrix • The matrix will have a 1 in position (i,j) if there is an edge from vertex i to vertex j. • The matrix has a 0 in position (i,j) if there is no edge between vertex i and vertex j. • An undirected graph will be symmetric

  5. Weighted Graph • Instead of using a 1 for a connection, put in the weight of that edge. • For some algorithms you could use +infinity or – infinity for no connection depending on the algorithm • Space requirements are |V|2 • This is fine if the matrix is dense (i.e. there are many edges compared to |V|2)

  6. Another Representation • If there are not many edges can use the adjacency list representation • Keep an array of head pointers of size |V| • Each element points to a linked list of vertices that are adjacent to the current vertex • The space requirements for this representation is |V|+|E| • If the edges have weights, we can store that information in the nodes of the linked list • Undirected graphs use 2x the storage

  7. Topological Sort • An ordering of vertices in a DAG such that if there is a path from vi to vj, then vj appears after vi in the ordering. • This is useful in determining the order to take courses if the DAG represents courses and their prerequisites • Cannot be done if there is a cycle • The ordering is not necessarily unique

  8. Topological Sort Algorithm • Find a vertex with no incoming edges • Print that vertex out • Remove that vertex from the graph and all the edges that go out of it • Repeat this process until there are no more vertices left • Will be useful to keep an indegree with each vertex • Update the indegree when you remove the node

  9. Analysis of Topological Sort • Find a vertex with no incoming edges takes O(|V|) • Will do this |V| times • Entire algorithm is O(|V|2) • Can make the find vertex more efficient • Keep a list of all vertices with indegree 0 • Find is now O(1) • When remove vertex and update indegrees, add to the list. • Now algorithm is O(|V|+|E|)

  10. Shortest Path Algorithms • Single source, shortest weighted path problem • Single source, shortest unweighted path problem • Shortest path to one vertex or all vertices • Assume no path has negative cost • Currently there is no algorithm to find the shortest path to a specific node that is any faster than finding the shortest path to all nodes

  11. Unweighted Shortest Paths • Find the shortest path from vertex s to all other vertices • Look at all vertices adjacent to s • Enter the value 1 in entries associated with those vertices • Look at all the vertices with an entry of 1 and find which vertices are adjacent to them. • Enter a 2 into those entries (that do not have a value entered). • Repeat until all vertices accounted for.

  12. Analysis of Algorithm • The longest path will be at most |V|. • Will repeat the loop at most |V| times. • The search for vertices with entry 1 is O(|V|). • Algorithm is O(|V|2). • Can be made more efficient by keeping a list for vertices with path length k and path length k+1. • As you go through the list of vertices with path length k, create list of vertices with path length k+1 to be used in the next loop. • Now O(|V|+|E|).

  13. Dijkstra’s Algorithm • Now what if the graph has weighted edges? • Use the same basic algorithm as with the unweighted graph. • Instead of keeping a value of the path length with each vertex, we keep a tentative path length. • Start the same way. Start at the starting node and see which nodes are adjacent to it.

  14. Shortest Weighted Path • Uses a greedy algorithm. • Greedy algorithms do not always work • This one does not work if a path weight is negative. • Keep a queue of vertices that can be processed (vertices adjacent to those that have been processed) and the weight to that vertex. Check to see if getting to that vertex is better than any other way to that vertex yet. Update the tentative path length if better. Add to queue if better. • Select the vertex with the smallest weight.

  15. Example from the book • Done on board

  16. Analysis • Using trivial find the minimum would take O(|V|) time to find the minimum. Do this |V| times and we get a O(|V|2) algorithm. • Really this is O(|E|+|V|2), but since |E|<=O(|V|2) we get the above result. • If |E| is much less than |V|2 then we could keep the list in a priority queue. • Now the algorithm is O(|E| log |V| + |V| log |V|) which is O(|E| log |V|). • The |E| log |V| term is from the insert.

  17. Minimum Spanning Tree • Find a tree formed from graph edges that connects all the vertices of G with lowest cost. • Can perform this on directed or undirected; weighted or unweighted graphs. • Result will be a tree since it will have no cycles. • We will look at weighed undirected graphs.

  18. Prim’s Algorithm • Pick any starting vertex. Place it in the tree. • Find all edges adjacent to all vertices in the tree. Pick the one with the smallest weight. • Do not pick an edge what would cause a cycle (the ending vertex is already in the tree). • Add that edge/vertex to the graph. • Repeat until all vertices are processed (if graph is connected).

  19. Kruskal’s Algorithm • Sort edges by weights. • Pick the next edge to add to the tree as the smallest edge not already picked that will not cause a cycle (the endpoint vertices are not both in the tree).

  20. Depth First Search • Start at some vertex • Visit an adjacent vertex and search all of its adjacent vertices before returning to the original vertex to search the rest of its adjacent vertices. • Must worry about cycles. • Easily implement as a preorder recursive general traversal with a marker for already visited (to prevent cycles).

More Related