Docsity

Graphs and Graph Algorithms: A Comprehensive Study - Prof. Nelson Padua-Perez, Study notes of Computer Science

Partial preview of the text

Download Graphs and Graph Algorithms: A Comprehensive Study - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

Graphs & Graph Algorithms 3

CMSC 132

Department of Computer ScienceUniversity of Maryland, College Park

Overview

Graph implementation^ Adjacency list / matrix / set Spanning trees Minimum spanning tree^ Prim’s algorithm^ Kruskal’s algorithm

Adjacency Matrix

Representation^ 2D array^ Position j, k

⇒⇒⇒⇒^ edge between nodes n

, njk

Unweighted graph^ Matrix elements

⇒⇒⇒⇒^ boolean Weighted graph^ Matrix elements

⇒⇒⇒⇒^ weight

Adjacency Matrix

Example

Adjacency List

Representation^ Linked or array list for each node ofneighbors/successors^ for directed graph, may need predecessors aswell^ Unweighted graph^ store neighbor^ Weighted graph^ store neighbor, weight

Adjacency List

Example^ Unweighted graph^ Weighted graph

Graph Space Requirements Adjacency matrix^2 ½ Nentries (for graph with N nodes, E edges) Many empty entries for large graphs Adjacency list E entries Adjacency Set/Map E entries Space overhead per entry higher than for adjacencylist

Graph Time Requirements

Average Complexity of operations^ For graph with N nodes, E edges

Adj Set/Map O(1)O(1)O(1) O(E/N) O(E/N) O(1) Find edge

O(E/N)

O(N)

Enumerateedges

O(E/N)

O(1)

Delete edge

O(E/N)

O(1)

Insert edge

Adj List Adj Matrix Operation

Recursive Spanning Tree

Construction

Known = { start }explore ( start );void explore (Node X) {^ for each successor Y of X

if (Y is not in Known)^ Parent[Y] = XAdd Y to Knownexplore(Y)

Spanning Tree Construction

Known = { start }Discovered = { start }while ( Discovered

≠≠≠≠ ∅∅∅∅^ )

take node X out of Discoveredfor each successor Y of X^ if (Y is not in Known)

Parent[Y] = XAdd Y to DiscoveredAdd Y to Known

Depth-First Spanning Tree Example

Breadth-First Spanning Tree Example

Minimum Spanning Tree (MST) Spanning tree with minimum total edge weight Multiple MSTs possible (with same weight)

Algorithms for MST

Two well known algorithms for minimumspanning tree^ developed independently Prim’s algorithm^ described in book Kruskal’s algorithm^ Not Clyde Kruskal (prof in our department, but hisuncle)