CS120 Graphs
GRAPHS
Graph Types Undirected Graphs Weighted undirected graph
Graph Terminology A graph and one of its subgraphs
Graph Terminology
Graph-like, but not Graphs
Running times for graph algorithms; expressed in terms of |V| and |E|. If |E| is |V2| then the graph is dense. If |E| is |V| then the graph is sparse.
Graphs as ADTs What data structure would you use? What operations should be supplied?
Graphs as ADTs ADT graph operations Test if empty Get number of vertices, edges in a graph See if edge exists between two given vertices Add vertex to graph whose vertices have distinct, different values from new vertex Add/remove edge between two given vertices Remove vertex, edges to other vertices Retrieve vertex that contains given value
GRAPH REPRESENTATIONS
Adjacency Matrix Representation
Matrix Example 2
Matrix Example 3 A directed unweighted graph and (b) its adjacency matrix
Matrix Example4 A weighted undirected graph and (b) its adjacency matrix
Matrix representation requires too much storage for large graphs that are sparse, but very efficient for small graphs: use 1 bit per edge.
Adjacency List Representation
List Example 2
List Example 3 A directed graph and (b) its adjacency list
List Example 4 A weighted undirected graph and (b) its adjacency list
Adjacency List Representation
Vertex Degree/Indegree/Outdegree What is the degree of vertex a? of vertex c? What is the indegree of vertex 0 ? Vertex 2 ? What is the outdegree of vertex 3? Vertex 1?
Adjacency List Representation
Graph Traversals Visits all vertices it can reach Visits all vertices if and only if the graph is connected Connected component Subset of vertices visited during a traversal that begins at a given vertex
Depth-First Search DFS traversal Goes as far as possible from a vertex before backing up Recursive traversal algorithm or stack based algorithm
Breadth-First Search BFS traversal Visits all vertices adjacent to a vertex before going forward BFS is a first visited, first explored strategy uses a queue based strategy Contrast DFS as last visited, first explored
Depth-First Search The results of a depth-first traversal, beginning at vertex a
Breadth-First Search The results of a breadth-first traversal, beginning at vertex a
Applications of Graphs Topological Sorting – finding an order of precedence relationships Spanning Tree – find a connected component Minimum Spanning Trees – find a minimum weight connected component Shortest Paths – find the shortest path from source to destination, find all pairs shortest paths Circuits – does a graph have a cycle Some Difficult Problems – Traveling Salesman Problem, Hamiltonian Circuit Problem, Euler Circuit Problem, Finding a k-Clique in a graph, etc…..
Topological Sorting A directed graph without cycles
Topological Sorting The graph arranged according to two topological orders
Topological Sorting
Topological Sorting Trace Depth First Search, output node to front of list when all its adjacent vertices have been visited.
Spanning Trees A tree is an undirected connected graph without cycles Detecting a cycle in an undirected graph Connected undirected graph with n vertices must have at least n – 1 edges If it has exactly n – 1 edges, it cannot contain a cycle With more than n – 1 edges, must contain at least one cycle
Spanning Trees A spanning tree for the graph. Dotted edges not in spanning tree.
Spanning Trees FIGURE 20-20 Connected graphs that each have four vertices and three edges
Spanning Trees Another possible spanning tree for the graph in
Spanning Trees The BFS spanning tree rooted at vertex a for the graph
Minimum Spanning Trees A weighted, connected, undirected graph