Download presentation
Presentation is loading. Please wait.
1
CS120 Graphs
2
GRAPHS
3
Graph Types Undirected Graphs Weighted undirected graph
4
Graph Terminology A graph and one of its subgraphs
5
Graph Terminology
6
Graph-like, but not Graphs
7
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.
8
Graphs as ADTs What data structure would you use? What operations should be supplied?
9
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
10
GRAPH REPRESENTATIONS
11
Adjacency Matrix Representation
12
Matrix Example 2
13
Matrix Example 3 A directed unweighted graph and (b) its adjacency matrix
14
Matrix Example4 A weighted undirected graph and (b) its adjacency matrix
15
Matrix representation requires too much storage for large graphs that are sparse, but very efficient for small graphs: use 1 bit per edge.
16
Adjacency List Representation
17
List Example 2
18
List Example 3 A directed graph and (b) its adjacency list
19
List Example 4 A weighted undirected graph and (b) its adjacency list
20
Adjacency List Representation
21
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?
22
Adjacency List Representation
23
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
24
Depth-First Search DFS traversal
Goes as far as possible from a vertex before backing up Recursive traversal algorithm or stack based algorithm
25
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
26
Depth-First Search The results of a depth-first traversal, beginning at vertex a
27
Breadth-First Search The results of a breadth-first traversal, beginning at vertex a
28
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…..
29
Topological Sorting A directed graph without cycles
30
Topological Sorting The graph arranged according to two topological orders
31
Topological Sorting
32
Topological Sorting Trace Depth First Search, output node to front of list when all its adjacent vertices have been visited.
33
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
34
Spanning Trees A spanning tree for the graph. Dotted edges not in spanning tree.
35
Spanning Trees FIGURE Connected graphs that each have four vertices and three edges
36
Spanning Trees Another possible spanning tree for the graph in
37
Spanning Trees The BFS spanning tree rooted at vertex a for the graph
38
Minimum Spanning Trees
A weighted, connected, undirected graph
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.