Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.

Similar presentations


Presentation on theme: "15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation."— Presentation transcript:

1 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation notes by David Murray

2 Definitions

3 15-211 Recitation notes on graphs, Uri Dekel and David Murray 3 What is a graph? G=(V,E)  V: a set of vertices (or “nodes”)  E: a set of edges (or “links”) Every edge has two endpoints In a directed graph, endpoint order matters  i.e., (v1,v2) != (v2, v1) In a weighted graph, every edge (and possibly node) has a weight.

4 15-211 Recitation notes on graphs, Uri Dekel and David Murray 4 Graph Examples Undirected graphs  Computer networks  Highway systems

5 15-211 Recitation notes on graphs, Uri Dekel and David Murray 5 Graph Examples Directed Graphs  Street maps with one-way and two-way streets  Game boards (e.g,”ladders and chutes”)

6 15-211 Recitation notes on graphs, Uri Dekel and David Murray 6 Graph Examples Weighted graphs  Computer networks e.g., Minimal time for packet routing  Highways with distances e.g., “What is the shortest driving distance from Pittsburgh to New York City?”

7 15-211 Recitation notes on graphs, Uri Dekel and David Murray 7 Graph Terms Loop  An edge whose two endpoints are the same Parallel edges  Two different edges with the same endpoints  A “simple graph” has no parallel edges

8 15-211 Recitation notes on graphs, Uri Dekel and David Murray 8 Graph Terms Path  A consecutive path of vertices and edges which starts at an “origin” vertex and ends at a “terminus” vertex Circuit  A path where the origin and terminus are the same Simple path  A path where no vertices are repeated (except origin in a simple circuit)

9 15-211 Recitation notes on graphs, Uri Dekel and David Murray 9 Graph Terms Connected graph  A graph where there is a path from every vertex to any other vertex

10 15-211 Recitation notes on graphs, Uri Dekel and David Murray 10 Trees Tree  A connected graph with no cycles i.e., any connected graph with no cycles is a tree TreeNot a tree

11 Programmatic Representation

12 15-211 Recitation notes on graphs, Uri Dekel and David Murray 12 Representing graphs Incidence matrix  Every row corresponds to a vertex  Every column corresponds to an edge  Entry is 1 if edge exists, 2 if loop  Use negatives to indicate direction

13 15-211 Recitation notes on graphs, Uri Dekel and David Murray 13 Representing simple graphs Adjacency matrix  Every row and column corresponds to a vertex  Entry corresponds to edges  Can use weights for given edges  Symmetric around diagonal if graph is undirected

14 15-211 Recitation notes on graphs, Uri Dekel and David Murray 14 Representing simple graphs Adjacency list  A set of linked lists or arrays, one for each vertex, listing the connected vertices or edges  Commonly used for sparse graphs

15 Mazes and Spanning Trees

16 15-211 Recitation notes on graphs, Uri Dekel and David Murray 16 Mazes A maze can be considered as a graph  Every “square” is a vertex  If there is no wall between two adjacent squares, an edge will link the corresponding vertices  There must be a single path from entrance vertex to exit vertex, and no cycle To construct a maze: create a spanning tree from the entrance vertex

17 15-211 Recitation notes on graphs, Uri Dekel and David Murray 17 Spanning tree The spanning of a graph G is a tree containing all vertices of G  More than one possible spanning tree

18 15-211 Recitation notes on graphs, Uri Dekel and David Murray 18 Minimum Spanning Tree A Minimum Spanning Tree of a graph G with weights on the edges  A spanning tree covering all the vertices of G with the lowest possible total weight  Note: there could be more than one MST, but all have the same weight

19 15-211 Recitation notes on graphs, Uri Dekel and David Murray 19 Kruskal’s Algorithm for MSTs Input:  Connected graph G=(V,E) and a set of weights Output:  Minimum spanning tree T=(V,E’) Algorithm: Start with empty T 1. Find edge e with smallest weight 2. If adding e to T does not create cycle, add it. 3. Continue until all vertices of G are in T. NOTE: Intermediate T is not necessarily a tree! (No cycles, but does not cover all of G)

20 15-211 Recitation notes on graphs, Uri Dekel and David Murray 20 Kruskal’s Example Input graph: Step #1: Choose an edge  (we’ll start with A-E although G-H is also 1)

21 15-211 Recitation notes on graphs, Uri Dekel and David Murray 21 Kruskal’s Example Input graph: Step #2: Choose G-H

22 15-211 Recitation notes on graphs, Uri Dekel and David Murray 22 Kruskal’s Example Input graph: Step #3: Choose B-F

23 15-211 Recitation notes on graphs, Uri Dekel and David Murray 23 Kruskal’s Example Input graph: Step #4: Choose B-C

24 15-211 Recitation notes on graphs, Uri Dekel and David Murray 24 Kruskal’s Example Input graph: Step #5: Choose F-G

25 15-211 Recitation notes on graphs, Uri Dekel and David Murray 25 Kruskal’s Example Input graph: Step #6: Choose E-F

26 15-211 Recitation notes on graphs, Uri Dekel and David Murray 26 Kruskal’s Example - Results Step #7 (Choose C-D): or… Step #7 (Choose H-D):

27 15-211 Recitation notes on graphs, Uri Dekel and David Murray 27 Prim’s Algorithm for MSTs Input:  Connected graph G=(V,E) and a set of weights  Vertex V0 to start from Output:  Minimum spanning tree T=(V,E’) rooted at V0 Algorithm: Start with T containing only V0 While there are still vertices not in T: Find minimal edge e between tree vertex and non-tree vertex Add e to T

28 Questions?


Download ppt "15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation."

Similar presentations


Ads by Google