Download presentation
Presentation is loading. Please wait.
Published byAlexander Hudson Modified over 6 years ago
1
C.Eng 213 Data Structures Graphs Fall Section 3
2
Graphs For expressing non-hierarchically related items Examples:
Networks: pipelines, roads, assignment problems Representing processes: flow charts, Markov models Representing partial orderings: PERT charts
3
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.
4
Graph Examples
5
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.,
6
Graph Examples
7
Representation
8
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; }
9
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); }
10
Depth-first Traversal
11
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);
12
Topological Sorting
13
Topological Sorting
14
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|)
15
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; } }
16
Shortest Paths: Dijkstra’s Algorithm
17
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)
18
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.
19
Minimum Spanning Trees by Prim’s Algorithm
20
Minimum Spanning Trees by Prim’s Algorithm
21
Minimum Spanning Trees by Prim’s Algorithm
22
Minimum Spanning Trees by Prim’s Algorithm
23
Minimum Spanning Trees by Prim’s Algorithm
24
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)
25
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; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.