Download presentation
Presentation is loading. Please wait.
1
Introduction (with applications) ADT & terminology
Graphs Introduction (with applications) ADT & terminology March 21, 2018 Cinda Heeren / Geoffrey Tien
2
Cinda Heeren / Geoffrey Tien
Total order 1 2 5 4 7 6 3 A B means A must go before B March 21, 2018 Cinda Heeren / Geoffrey Tien
3
Cinda Heeren / Geoffrey Tien
Partial order Getting dressed pants shirt socks coat undies belt watch boots pocket knife March 21, 2018 Cinda Heeren / Geoffrey Tien
4
Cinda Heeren / Geoffrey Tien
Topological sort Given a graph, 𝐺= 𝑉,𝐸 , output all vertices in V such that no vertex is output before any other vertex with an edge to it Classic application: course prerequisites CPSC 110 CPSC 121 MATH 100 MATH 101 CPSC 210 CPSC 221 STAT 241 MATH 200 MATH 221 CPSC 213 CPSC 310 CPSC 320 CPSC 313 March 21, 2018 Cinda Heeren / Geoffrey Tien
5
Cinda Heeren / Geoffrey Tien
Topological sort Label each vertex's in-degree (# of inbound edges) Initialize a queue to contain all vertices with in-degree zero While there are vertices remaining in the queue Pick a vertex 𝑣 from the queue and output it Reduce the in-degree of all vertices adjacent to 𝑣 Put any of these with updated zero in-degree on the queue Remove 𝑣 from the queue March 21, 2018 Cinda Heeren / Geoffrey Tien
6
Cinda Heeren / Geoffrey Tien
Topological sort Queue: 1 6 Vertex In-degree 1 2 3 4 5 6 2 4 5 1 6 3 March 21, 2018 Cinda Heeren / Geoffrey Tien
7
Cinda Heeren / Geoffrey Tien
Topological sort Queue: 1 6 2 Vertex In-degree 1 2 3 4 5 6 2 4 5 1 6 3 1 1 6 March 21, 2018 Cinda Heeren / Geoffrey Tien
8
Cinda Heeren / Geoffrey Tien
Topological sort Queue: 2 3 Vertex In-degree 1 2 3 4 5 6 2 4 5 1 6 3 1 6 2 March 21, 2018 Cinda Heeren / Geoffrey Tien
9
Cinda Heeren / Geoffrey Tien
Topological sort Queue: 3 4 Vertex In-degree 1 2 3 4 5 6 2 4 5 1 6 3 1 1 6 2 3 March 21, 2018 Cinda Heeren / Geoffrey Tien
10
Cinda Heeren / Geoffrey Tien
Topological sort Queue: 4 5 Vertex In-degree 1 2 3 4 5 6 2 4 5 1 6 3 1 6 2 3 4 5 March 21, 2018 Cinda Heeren / Geoffrey Tien
11
Cinda Heeren / Geoffrey Tien
Graph applications Graphs are used as representations of many types of problems Computer network configuration Airline flight booking Pathfinding algorithms Database dependencies Task scheduling Road traffic modeling … March 21, 2018 Cinda Heeren / Geoffrey Tien
12
Cinda Heeren / Geoffrey Tien
Exam scheduling CPSC 221 CPSC 210 BIOL 201 ECON 211 MATH 200 MATH 223 Vertices represent courses Edges represent conflicts (at least one student is enrolled in both courses) Colours represent time slots Can we colour the graph (ideally using a limited number of colours) such that vertices joined by an edge do not have the same colour? March 21, 2018 Cinda Heeren / Geoffrey Tien
13
Cinda Heeren / Geoffrey Tien
Finite state automata Begin in the start vertex at the top for each digit d in the given number, follow d blue (solid) edges in succession after processing one digit, follow 1 red (dashed) edge If you end up back at the start node, the number is divisible by 7 e.g. 3703 YES March 21, 2018 Cinda Heeren / Geoffrey Tien
14
Cinda Heeren / Geoffrey Tien
Navigation Find a driving route(s) from location A to B March 21, 2018 Cinda Heeren / Geoffrey Tien
15
Cinda Heeren / Geoffrey Tien
Pathfinding Movement/position of video game elements, around terrain/obstacles etc. March 21, 2018 Cinda Heeren / Geoffrey Tien
16
Cinda Heeren / Geoffrey Tien
Medical imaging Automatic segmentation of anatomy in medical images March 21, 2018 Cinda Heeren / Geoffrey Tien
17
Cinda Heeren / Geoffrey Tien
Graphics Stanford bunny March 21, 2018 Cinda Heeren / Geoffrey Tien
18
Cinda Heeren / Geoffrey Tien
Game theory Game states and searching for winning sequence of moves March 21, 2018 Cinda Heeren / Geoffrey Tien
19
Cinda Heeren / Geoffrey Tien
Graph ADT Graphs are a formalism useful for representing relationships between things A graph 𝐺 is represented as 𝐺= 𝑉,𝐸 𝑉 is a set of vertices: 𝑣 1 , 𝑣 2 ,…, 𝑣 𝑛 E is a set of edges: 𝑒 1 , 𝑒 2 ,…, 𝑒 𝑚 where each 𝑒 𝑖 connects two vertices 𝑣 𝑖1 , 𝑣 𝑖2 𝑉= 𝑇𝑜𝑚, 𝑆ℎ𝑒𝑙𝑙𝑦, ℎ𝑎𝑚𝑠𝑡𝑒𝑟, 𝑝𝑜𝑝𝑐𝑜𝑟𝑛 𝐸= 𝑇𝑜𝑚, 𝑆ℎ𝑒𝑙𝑙𝑦 , 𝑆ℎ𝑒𝑙𝑙𝑦, 𝑇𝑜𝑚 , 𝑆ℎ𝑒𝑙𝑙𝑦,ℎ𝑎𝑚𝑠𝑡𝑒𝑟 , ℎ𝑎𝑚𝑠𝑡𝑒𝑟,𝑝𝑜𝑝𝑐𝑜𝑟𝑛 Tom Shelly hamster popcorn March 21, 2018 Cinda Heeren / Geoffrey Tien
20
Cinda Heeren / Geoffrey Tien
Graph ADT Operations Operations might include: Creation (with a certain number of vertices) Iterate through all vertices in the graph (traversal) Iterating over vertices adjacent to a specific vertex Asking whether an edge exists connecting two vertices Ask about the weight/cost of an edge between two vertices Inserting/removing edges (other operations/algorithms that may be useful) March 21, 2018 Cinda Heeren / Geoffrey Tien
21
Cinda Heeren / Geoffrey Tien
Graph terminology Paths, vertices, edges A graph consists of two sets, 𝑉 and 𝐸 Two vertices may be connected by a path A sequence of edges that begins at one vertex and ends at the other A simple path does not pass through the same vertex more than once A cycle is a path that starts and ends at the same vertex If a graph has v vertices, how many edges does it have? If every vertex is connected to every other vertex, and we count each direction as two edges v2 – v If the graph is a tree v – 1 Minimum number of edges March 21, 2018 Cinda Heeren / Geoffrey Tien
22
Connected / unconnected graphs
A connected graph is one where every pair of distinct vertices has a path between them A complete graph is one where every pair of vertices has an edge between them A graph (usually) cannot have multiple edges between the same pair of vertices A graph (usually) cannot have self edges i.e. an edge from and to the same vertex connected graph complete graph unconnected graph March 21, 2018 Cinda Heeren / Geoffrey Tien
23
Cinda Heeren / Geoffrey Tien
Directed graphs In a directed graph (or digraph) each edge has a direction and is called a directed edge A directed edge can only be traveled in one direction A pair of vertices in a digraph may have two edges between them, one in each direction March 21, 2018 Cinda Heeren / Geoffrey Tien
24
Cinda Heeren / Geoffrey Tien
Weighted graphs In a weighted graph each edge is assigned a weight Edges are labeled with their weights Each edge’s weight represents the cost to travel along that edge The cost could be distance, time, money or some other measure The cost depends on the underlying problem 1 2 4 3 5 March 21, 2018 Cinda Heeren / Geoffrey Tien
25
Readings for this lesson
Carrano & Henry Chapter 20.1 – 20.2 (Graph terminology, ADT) Next class: Carrano & Henry, Chapter 20.2 – 20.3 (implementation, traversals) March 21, 2018 Cinda Heeren / Geoffrey Tien
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.