C.Eng 213 Data Structures Graphs Fall 2008 2009 Section 3.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph Search Applications, Minimum Spanning Tree
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Chapter 5 : Trees.
Single-Source Shortest Paths
Minimum Spanning Trees
CSE 2331/5331 Topic 9: Basic Graph Alg.
Cse 373 May 8th – Dijkstras.
CS202 - Fundamental Structures of Computer Science II
12. Graphs and Trees 2 Summary
Spanning Trees Lecture 21 CS2110 – Fall 2016.
CISC 235: Topic 10 Graph Algorithms.
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Refresh and Get Ready for More
Graph Algorithm.
Graphs (picture above from ) 7/29/2009
Graphs Representation, BFS, DFS
Minimum Spanning Tree Neil Tang 3/25/2010
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
2018, Fall Pusan National University Ki-Joune Li
Shortest Path Algorithms
Chapter 11 Graphs.
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
2017, Fall Pusan National University Ki-Joune Li
Minimum Spanning Tree Neil Tang 4/3/2008
CSCI2100 Data Structures Tutorial
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CE 221 Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
Graphs.
Graphs.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Graphs.
GRAPH – Definitions A graph G = (V, E) consists of
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

C.Eng 213 Data Structures Graphs Fall 2008 2009 Section 3

Graphs For expressing non-hierarchically related items Examples: Networks: pipelines, roads, assignment problems Representing processes: flow charts, Markov models Representing partial orderings: PERT charts

Graphs A graph consists of Node set V = {v0, . . .}, and edge set E. A set of nodes (vertices ) A set of edges: pairs of nodes. Nodes with an edge between are called adjacent. Depending on problem, nodes or edges may have labels (or weights ) Node set V = {v0, . . .}, and edge set E. If the edges have an order (first, second), they are directed edges, and we have a directed graph (digraph), otherwise an undirected graph. Edges are incident to their nodes. Directed edges exit one node and enter the next. A cycle is a path without repeated edges leading from a node back to itself (following arrows if directed). A graph is cyclic if it has a cycle, else acyclic. DirectedAcyclic Graph—DAG.

Graph Examples

Graphs An undirected graph is connected if there is a path between every pair of nodes. That is, if one node of the pair is reachable from the other. If a directed graph has this property, it is called strongly connected If a directed graph becomes connected when arc directions are neglected, it is called weakly connected. A complete graph is a graph in which there is an edge between every pair of vertices A DAG is a (rooted) tree iff connected, and every node but the root has exactly one parent. A connected, acyclic, undirected graph is also called a free tree. Free: we’re free to pick the root; e.g.,

Graph Examples

Representation

Traversal COLLECTION OF VERTICES fringe; fringe = INITIAL COLLECTION; while (! fringe.isEmpty()) { Vertex v = fringe.REMOVE HIGHEST PRIORITY ITEM(); if (! MARKED(v)) { MARK (v); VISIT (v); For each edge (v,w) { if (NEEDS PROCESSING (w)) Add w to fringe; }

Depth-first Traversal Problem: Visit every node reachable from v once, visiting nodes further from start first. Stack<Vertex> fringe; fringe = stack containing {v}; while (! fringe.isEmpty()) { Vertex v = fringe.pop (); if (! marked (v)) { mark (v); VISIT(v); For each edge (v,w) { if (! marked (w)) fringe.push (w); }

Depth-first Traversal

Recursive Depth-First Traversal void traverse (Graph G) { for (v in nodes of G) { traverse (G, v); } void traverse (Graph G, Node v) { if (v is unmarked) { mark (v); visit v; for (Edge (v, w) in G) traverse (G, w);

Topological Sorting

Topological Sorting

Topological Sorting Analysis: Finding the node with no predecessor O(|V|) // scan the array of vertices Repeat this for V nodes O(|V|2) When you keep set of nodes with no predecessor (with in-degree zero) in a stack or queue and use adjacency list O(|E|+|V|)

Shortest Paths: Dijkstra’s Algorithm Problem: Given a graph (directed or undirected) with non-negative edge weights, compute shortest paths from given source node, s, to all nodes. (Single source shortest path problem) • “Shortest” = sum of weights along path is smallest. • For each node, keep estimated distance from s, . . . • . . . and of preceding node in shortest path from s. PriorityQueue<Vertex> fringe; For each node v { v.dist() = ∞; v.back() = null; } s.dist() = 0; fringe = priority queue ordered by smallest .dist(); add all vertices to fringe; while (! fringe.isEmpty()) { Vertex v = fringe.removeFirst (); For each edge (v,w) { if (v.dist() + weight(v,w) < w.dist()) { w.dist() = v.dist() + weight(v,w); w.back() = v; } }

Shortest Paths: Dijkstra’s Algorithm

Shortest Paths: Dijkstra’s Algorithm Analysis: With priority queue O((|E|+|V|) log |V|) → O(|E|log|V|) Without priority queue O(|E|+|V|2) → O(|V|2)

Minimum Spanning Tree Problem: Given a set of places and distances between them (assume always positive), find a set of connecting roads of minimum total length that allows travel between any two. The routes you get will not necessarily be shortest paths. Easy to see that such a set of connecting roads and places must form a tree, because removing one road in a cycle still allows all to be reached.

Minimum Spanning Trees by Prim’s Algorithm

Minimum Spanning Trees by Prim’s Algorithm

Minimum Spanning Trees by Prim’s Algorithm

Minimum Spanning Trees by Prim’s Algorithm

Minimum Spanning Trees by Prim’s Algorithm

Minimum Spanning Trees by Prim’s Algorithm Analysis: With priority queue O((|E|+|V|) log |V|) → O(|E|log|V|) Without priority queue O(|E|+|V|2) → O(|V|2)

Minimum Spanning Trees by Kruskal’s Algorithm • Observation: the shortest edge in a graph can always be part of a minimum spanning tree. • In fact, if we have a bunch of subtrees of a MST, then the shortest edge that connects two of them can be part of a MST, combining the two subtrees into a bigger one. • So,. . . Create one (trivial) subtree for each node in the graph; MST = {}; for each edge (v,w), in increasing order of weight { if ( (v,w) connects two different subtrees ) { Add (v,w) to MST; Combine the two subtrees into one; }