-
Notifications
You must be signed in to change notification settings - Fork 1
/
edge_list.cpp
100 lines (86 loc) · 2.03 KB
/
edge_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
g++ self_loops.cpp -o g
echo "2
1
4 5 1
0 3
3 1 4
3 0" | ./g
. . * . . .
. . . . . .
. * . . * *
* . . . . .
. * . * . .
* . . * . .
Notes:
pair comparison
range delete
erase returns new iterator
*/
#define _GLIBCXX_DEBUG
#define _SECURE_SCL 1
#include <vector>
#include <string>
#include <cassert>
#include <iterator>
#include <algorithm>
#include <istream>
#include <ostream>
#include <sstream>
#include <iostream>
#include <utility>
#include <numeric>
// Adjacency list graph representation
typedef unsigned vertex_id;
typedef float edge_weight;
typedef std::pair<vertex_id, vertex_id> edge;
typedef std::vector<edge> graph;
// True iff there is an edge in g from u to v
// Complexity: O( log(|V|) )
inline bool has_edge(graph const& g, int u, int v)
{
$writeme$
}
// Add an edge in g from u to v with weight w
// Complexity: O( log(|V|) )
// Requires: u is a vertex in g, i.e. u < count_vertices( g )
inline void add_edge( graph& g, vertex_id u, vertex_id v )
{
$writeme$
}
// Background: to remove an element from a vector<T> v, invoke
// v.erase(x) where x is a vector<T>::iterator to the element
// Write a function delete_edge( g, u, v ) that uses v.erase with
// std::find to delete an edge from the graph
inline void delete_edge( graph& g, vertex_id u, vertex_id v )
{
$writeme$
}
// Write another function delete_self_loops( g ) that uses del_edge to
// delete all self-loops from the graph
inline void delete_self_loops( graph& g )
{
$writeme$
}
// Read a graph from input in adjacency list form.
void read_edge_list( graph& g )
{
$writeme$
}
// Write a g to output in adjacency matrix form.
void write_adjacency_matrix( graph const& g )
{
$fixme$
for ( vertex_id u = 0; u < count_vertices( g ); ++u )
{
for ( vertex_id v = 0; v < count_vertices( g ); ++v )
std::cout << (has_edge( g, u, v ) ? "* " : ". ");
std::cout << std::endl;
}
}
int main( int argc, char *argv[] )
{
graph g;
read_edge_list( g );
write_adjacency_matrix( g );
}